I've recently decided to move my game from a top down view to a more 2d view with animated sprites and I've got the main sprite animated using AniSprite, apart when I've put it into my main game .hpp to play the animation when the character moves.
I've spent about 5 hours trying to get it to work, tried everything I could think off to get it to move when I hold down an arrow key, even rewriting most of this part of the program.
The thing is, is that when I move left for example, I want the animation to keep on playing until I release the left key, here's the initial movement coding:
if (cantmove == 0) ///cant move if 1
{
if(App.GetInput().IsKeyDown(sf::Key::Left))
{
map.Move(speed,0); map3.Move(speed,0);
guy.Move(speed,0); woman.Move(speed,0);
woman2.Move(speed,0);
if (Collision::PixelPerfectTest(map, aniCross)
|| Collision::PixelPerfectTest(guy, aniCross) || Collision::PixelPerfectTest(woman, aniCross) || Collision::PixelPerfectTest(woman2, aniCross))
{
map.Move(-speed,0); map3.Move(-speed,0);
guy.Move(-speed,0); woman.Move(-speed,0);
woman2.Move(-speed,0);
}
}
if(App.GetInput().IsKeyDown(sf::Key::Right))
{
map.Move(-speed,0); map3.Move(-speed,0);
guy.Move(-speed,0); woman.Move(-speed,0);
woman2.Move(-speed,0);
if (Collision::PixelPerfectTest(map, aniCross) || Collision::PixelPerfectTest(guy, aniCross) || Collision::PixelPerfectTest(woman, aniCross)
|| Collision::PixelPerfectTest(woman2, aniCross))
{
map.Move(speed,0); map3.Move(speed,0);
guy.Move(speed,0); woman.Move(speed,0);
woman2.Move(speed,0);
}
}
if(App.GetInput().IsKeyDown(sf::Key::Up))
{
map.Move(0,speed); map3.Move(0,speed);
guy.Move(0,speed); woman.Move(0,speed);
woman2.Move(0,speed);
if (Collision::PixelPerfectTest(map, aniCross) || Collision::PixelPerfectTest(guy, aniCross)
|| Collision::PixelPerfectTest(woman, aniCross) || Collision::PixelPerfectTest(woman2, aniCross))
{
map.Move(0,-speed); map3.Move(0,-speed);
guy.Move(0,-speed); woman.Move(0,-speed);
woman2.Move(0,-speed);
}
}
if(App.GetInput().IsKeyDown(sf::Key::Down))
{
map.Move(0,-speed); map3.Move(0,-speed);
guy.Move(0,-speed) ;woman.Move(0,-speed);
woman2.Move(0,-speed);
if (Collision::PixelPerfectTest(map, aniCross) || Collision::PixelPerfectTest(guy, aniCross)
|| Collision::PixelPerfectTest(woman, aniCross) || Collision::PixelPerfectTest(woman2, aniCross))
{
map.Move(0,speed); map3.Move(0,speed);
guy.Move(0,speed); woman.Move(0,speed);
woman2.Move(0,speed);
}
}
}
*aniCross is the crosshair which is used as the center of the screen and the character; guy, woman and woman2 are npcs*
Then I put in this piece of code, which gets it to do the animation, but only once:
if (App.GetEvent(Event))
{
if (Event.Type == sf::Event::KeyPressed)
{
switch (Event.Key.Code)
{
case sf::Key::Left:
aniCross.Play(3, 6);
// break;
}
}
if (Event.Type == sf::Event::KeyReleased)
{
switch (Event.Key.Code)
{
case sf::Key::Left:
aniCross.Stop();
//break; not making a difference
}
}
}
I put this in just before the last bracket in the first code, and the peculiar thing is that when holding left, it will go through the 3 frames once then leave me with a static image of the first, but if i'm holding left, and then tap up or down, it will start looping like i want it to. Very weird indeed :/
(I've also tried placing it everywhere else in the program, but this is the best place so far)
It would be awesome if someone could help with this, otherwise my only other option would be to copy and paste the sprite sheet onto itself so it will be one massive picture, but that will be inefficient, it will eventually end, and it's the lazy way out
Cheers in advance!
Jake
P.S I can also give out the entire source code if that's needed! (be on tomorrow)
EDIT: I removed the (3, 6) from aniCross.Play() and found out that it will only play 3 sprites altogether, maybe some problem with it accessing it?