First of all, thank you creating such a useful tool. It is much appreciated
.
Alright, now, I've been trying to figure out a way to handle collision with your code, but I'm coming up short. If I create more than 2 layers on a Tiled map, the layers don't look right when they load. So, I made 1 .tmx with 2 layers (which is all visable tiles), and then a second .tmx with a single layer with invisible tiles (for collision). I've been trying every method I could find to load the tiles from the Collision.tmx and not allow the animatedSprite to pass through them, but nothing is working. Here is my code at the moment:
#include "stdafx.h"
#include "zlib.h"
#include "MapLoader.h"
#include "AnimatedSprite.h"
#include "Animation.h"
#include "zconf.h"
#include <luabind/luabind.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
int main()
{
sf::Vector2i screenDimensions(720, 420);
sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Pokemon Clone");
// load texture
sf::Texture texture;
texture.loadFromFile("Player.png");
//create map loader and load map
tmx::MapLoader ml("Maps/");
tmx::MapLoader mll("Maps/");
ml.Load("Pallet Town.tmx");
mll.Load("Collision.tmx");
// push frames
Animation walkingAnimationDown;
walkingAnimationDown.setSpriteSheet(texture);
walkingAnimationDown.addFrame(sf::IntRect(16, 0, 16, 20));
walkingAnimationDown.addFrame(sf::IntRect(32, 0, 16, 20));
walkingAnimationDown.addFrame(sf::IntRect(16, 0, 16, 20));
walkingAnimationDown.addFrame(sf::IntRect(0, 0, 16, 20));
Animation walkingAnimationLeft;
walkingAnimationLeft.setSpriteSheet(texture);
walkingAnimationLeft.addFrame(sf::IntRect(16, 40, 16, 20));
walkingAnimationLeft.addFrame(sf::IntRect(32, 40, 16, 20));
walkingAnimationLeft.addFrame(sf::IntRect(16, 40, 16, 20));
walkingAnimationLeft.addFrame(sf::IntRect(0, 40, 16, 20));
Animation walkingAnimationRight;
walkingAnimationRight.setSpriteSheet(texture);
walkingAnimationRight.addFrame(sf::IntRect(16, 60, 16, 20));
walkingAnimationRight.addFrame(sf::IntRect(32, 60, 16, 20));
walkingAnimationRight.addFrame(sf::IntRect(16, 60, 16, 20));
walkingAnimationRight.addFrame(sf::IntRect(0, 60, 16, 20));
Animation walkingAnimationUp;
walkingAnimationUp.setSpriteSheet(texture);
walkingAnimationUp.addFrame(sf::IntRect(16, 20, 16, 20));
walkingAnimationUp.addFrame(sf::IntRect(32, 20, 16, 20));
walkingAnimationUp.addFrame(sf::IntRect(16, 20, 16, 20));
walkingAnimationUp.addFrame(sf::IntRect(0, 20, 16, 20));
// set up AnimatesSprite
AnimatedSprite animatedSprite(sf::seconds(0.13));
animatedSprite.setAnimation(walkingAnimationDown);
animatedSprite.pause();
animatedSprite.setPosition(700, 400);
sf::Clock frameClock;
sf::Music music;
if(!music.openFromFile("Pallet Town Theme.ogg"))
std::cout << "ERROR: Can't find Pallet Town Theme.ogg" << std::endl;
music.play();
music.setLoop(true);
float speed = 0.045f;
bool bNoKeyWasPressed = true;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
if (event.type == sf::Event::LostFocus)
{
music.pause();
}
if (event.type == sf::Event::GainedFocus)
music.play();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if(animatedSprite.getAnimation() != &walkingAnimationUp)
{
animatedSprite.setAnimation(walkingAnimationUp);
}
animatedSprite.play();
animatedSprite.move(0, -speed);
animatedSprite.move(0, 0);
bNoKeyWasPressed = false;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if(animatedSprite.getAnimation() != &walkingAnimationDown)
{
animatedSprite.setAnimation(walkingAnimationDown);
}
animatedSprite.play();
animatedSprite.move(0, speed);
bNoKeyWasPressed = false;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if(animatedSprite.getAnimation() != &walkingAnimationRight)
{
animatedSprite.setAnimation(walkingAnimationRight);
}
animatedSprite.play();
animatedSprite.move(speed, 0);
bNoKeyWasPressed = false;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if(animatedSprite.getAnimation() != &walkingAnimationLeft)
{
animatedSprite.setAnimation(walkingAnimationLeft);
}
animatedSprite.play();
animatedSprite.move(-speed, 0);
bNoKeyWasPressed = false;
}
else if(bNoKeyWasPressed)
{
animatedSprite.pause();
}
bNoKeyWasPressed = true;
// update AnimatedSprite
animatedSprite.update(frameClock.restart());
window.clear();
window.draw(ml);
ml.Draw(window, tmx::MapLayer::Back);
window.draw(animatedSprite);
ml.Draw(window, tmx::MapLayer::Front);
mll.Draw(window, tmx::MapLayer::All);
window.display();
}
return 0;
}