Hello everyone. I am using the TMX Map Loader to load the .tmx files from the Tiled Map Editor. In the documentation they have a section covering collision detection with this snippet of code:
bool collision;
for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
{
if(layer->name == "Test")
{
for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
{
collision = object->Contains(point);
}
}
}
For the line collision = object->Contains(point);
, am I right to use something along the lines of animatedSprite.getPosition() in place of point? Also, when I try to use that code to determine whether or not Collision is true or false, it is always true. For example, if I make a move condition for my sprite stating that they can move if collision is false, else they cannot move, they will never be able to move.
I'm sure this didn't come out as clearly as it sounded in my head. Any help with this would be greatly appreciated!
The more I look at this the more it doesn't make sense to me. Collision will always be true as far as I can tell because it's initially set to be true, and then all the conditions that would change it are inside of those loops, but once the loops cease to run collision will still be true.
Here is some code:
// set up AnimatesSprite
AnimatedSprite animatedSprite(sf::seconds(0.13));
animatedSprite.setAnimation(walkingAnimationDown);
animatedSprite.pause();
animatedSprite.setPosition(700, 400);
bool collision;
for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
{
if(layer->name == "Test")
{
for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
{
collision = object->Contains(animatedSprite.getPosition());
}
}
}
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();
if(collision = false)
animatedSprite.move(0, -speed);
else
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;
}