Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite Animation Speed Ridiculously Fast  (Read 13812 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #15 on: July 18, 2013, 12:31:41 pm »
Glad to hear my class is of use to somebody! :)
Take a look at the AnimatedSprite.hpp. You can either set the animation looping directly in the constuctor (little bit more efficient) or use the setLooped(bool) method.
Your code looks alright so far. Although I wouldn't use events for controlling the animation. Put it with the moving code and use the keyboard directly.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #16 on: July 18, 2013, 01:04:06 pm »
I am having a hard time understanding where to put this bool still... I don't even know what it is that I am not understanding,

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #17 on: July 18, 2013, 01:11:53 pm »
You just got me thinking... The animatedSprite is looped by default. So there must be something else wrong with your code.
Please move the animation code from the events to the realtime input and see if it changes anything.

edit: i think this is causing the problem:
else
{
    animatedSprite.pause();
}
because it pauses the animatedSprite if any other event happens.
« Last Edit: July 18, 2013, 01:14:10 pm by Foaly »

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #18 on: July 18, 2013, 01:18:16 pm »
Moving the animations to input has no animation working at all. Leaving them inside events with the else commented out still only does one loop, but as soon as I let go of the key, the animation just keeps going.

Also, if I continue to hold down the key, no other event is happening, so the animation should keep playing with that else statement. It should only trigger if I do anything else at that point.
« Last Edit: July 18, 2013, 01:31:50 pm by andrewhopps »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #19 on: July 18, 2013, 01:35:37 pm »
I can't help without any code. The animation code really doesn't belong in the event loop. Show us the code that is not working and I'll see if I can find out why.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #20 on: July 18, 2013, 01:44:01 pm »
That is the movement code...
Current situation with this setup:
The animation does not play when I press any direction key, upon release of the key, the animation begins to play the last direction it was going but it is going in loop.

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                animatedSprite.move(0, -speed);
                animatedSprite.setAnimation(walkingAnimationUp);
                animatedSprite.play();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                animatedSprite.move(0, speed);
                animatedSprite.setAnimation(walkingAnimationDown);
                animatedSprite.play();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
                animatedSprite.move(speed, 0);
                animatedSprite.setAnimation(walkingAnimationRight);
                animatedSprite.play();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                animatedSprite.move(-speed, 0);
                animatedSprite.setAnimation(walkingAnimationLeft);
                animatedSprite.play();
            }

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #21 on: July 18, 2013, 02:17:23 pm »
Thank you. Could you also provide the spritesheet, so I can do some testing myself?

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #22 on: July 18, 2013, 02:21:48 pm »
Spritesheet being used currently

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #23 on: July 18, 2013, 04:35:35 pm »
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                animatedSprite.move(-speed, 0);
                animatedSprite.setAnimation(walkingAnimationLeft);
                animatedSprite.play();
            }
I'm not sure what animatedSprite.play() does, but could it be possible that while you are holding sf::Keyboard::Left to move Left, the animation keeps starting over in Play? if this is the case, then when you finally let go of the key the animation will finally start it's full cycle.
« Last Edit: July 18, 2013, 04:44:43 pm by Gobbles »

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #24 on: July 18, 2013, 04:52:33 pm »
That's actually a really good idea! That is probably the issue. Now for a solution...

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #25 on: July 18, 2013, 04:55:20 pm »
That is exactly the issue, but it's not caused by play(), but by setAnimation()
I think I have a solution. Just let me test it first.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #26 on: July 18, 2013, 05:06:50 pm »
You should invest some time in separating Animation updates and input altogether. Input should be handled by it's own, and when you get into updating Animations(which would come in the general character update) you just check for any pressed keys, if no keys are pressed, then you revert to an idle animation, stop any current animations or even let the current animation finish what it's doing. This way if you come into anything more then just 4 movement animations, things will be easier to add in.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #27 on: July 18, 2013, 05:11:26 pm »
You should invest some time in separating Animation updates and input altogether. Input should be handled by it's own, and when you get into updating Animations(which would come in the general character update) you just check for any pressed keys, if no keys are pressed, then you revert to an idle animation, stop any current animations or even let the current animation finish what it's doing. This way if you come into anything more then just 4 movement animations, things will be easier to add in.

At my current understanding of c++ and SFML, I am doing what I know how to do :)

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Sprite Animation Speed Ridiculously Fast
« Reply #28 on: July 19, 2013, 01:03:13 pm »
Ok so this actually turned out harder than I thought it would be. This is what I came up with so far:
#include <SFML/Graphics.hpp>
#include "AnimatedSprite.hpp"

int main()
{
    sf::Vector2i screenDimensions(800,600);
    sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Animations!");

    // load texture
    sf::Texture texture;
    texture.loadFromFile("Player.png");

    // push frames
    Animation walkingAnimationDown;
    walkingAnimationDown.setSpriteSheet(texture);
    walkingAnimationDown.addFrame(sf::IntRect(0, 0, 32, 32));
    walkingAnimationDown.addFrame(sf::IntRect(32, 0, 32, 32));
    walkingAnimationDown.addFrame(sf::IntRect(64, 0, 32, 32));
    walkingAnimationDown.addFrame(sf::IntRect(32, 0, 32, 32));

    Animation walkingAnimationLeft;
    walkingAnimationLeft.setSpriteSheet(texture);
    walkingAnimationLeft.addFrame(sf::IntRect(0, 32, 32, 32));
    walkingAnimationLeft.addFrame(sf::IntRect(32, 32, 32, 32));
    walkingAnimationLeft.addFrame(sf::IntRect(64, 32, 32, 32));
    walkingAnimationLeft.addFrame(sf::IntRect(32, 32, 32, 32));

    Animation walkingAnimationRight;
    walkingAnimationRight.setSpriteSheet(texture);
    walkingAnimationRight.addFrame(sf::IntRect(0, 64, 32, 32));
    walkingAnimationRight.addFrame(sf::IntRect(32, 64, 32, 32));
    walkingAnimationRight.addFrame(sf::IntRect(64, 64, 32, 32));
    walkingAnimationRight.addFrame(sf::IntRect(32, 64, 32, 32));

    Animation walkingAnimationUp;
    walkingAnimationUp.setSpriteSheet(texture);
    walkingAnimationUp.addFrame(sf::IntRect(0, 96, 32, 32));
    walkingAnimationUp.addFrame(sf::IntRect(32, 96, 32, 32));
    walkingAnimationUp.addFrame(sf::IntRect(64, 96, 32, 32));
    walkingAnimationUp.addFrame(sf::IntRect(32, 96, 32, 32));


    // set up AnimatesSprite
    AnimatedSprite animatedSprite(sf::seconds(0.2));
    animatedSprite.setAnimation(walkingAnimationDown);
    animatedSprite.pause();
    animatedSprite.setPosition(0,0);

    sf::Clock frameClock;

    float speed = 0.01f;
    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(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationUp)
            {
                 animatedSprite.setAnimation(walkingAnimationUp);
            }
            animatedSprite.play();
            animatedSprite.move(0, -speed);
            bNoKeyWasPressed = false;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationDown)
            {
                animatedSprite.setAnimation(walkingAnimationDown);
            }
            animatedSprite.play();
            animatedSprite.move(0, speed);
            bNoKeyWasPressed = false;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationRight)
            {
                animatedSprite.setAnimation(walkingAnimationRight);
            }
            animatedSprite.play();
            animatedSprite.move(speed, 0);
            bNoKeyWasPressed = false;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationLeft)
            {
                animatedSprite.setAnimation(walkingAnimationLeft);
            }
            animatedSprite.play();
            animatedSprite.move(-speed, 0);
            bNoKeyWasPressed = false;
        }

        if(bNoKeyWasPressed)
        {
            animatedSprite.pause();
        }
        bNoKeyWasPressed = true;

        // update AnimatedSprite
        animatedSprite.update(frameClock.restart());


        window.clear();
        window.draw(animatedSprite);
        window.display();
    }

    return 0;
}
 
Also AnimatedSprite has to be extended by the addition of this method
const Animation* AnimatedSprite::getAnimation() const
{
    return m_animation;
}
And you have to delete the line m_currentTime = sf::Time::Zero; in AnimatedSprite::play() (I don't know why I put it there in the first place...)

It doesn't work for diagonal movement, but I don't have time to think of something clean and elegant right now. Also I'm not gonna be around a computer for the next 2 weeks, so I though I share this, so that somebody else can maybe improve it.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Sprite Animation Speed Ridiculously Fast
« Reply #29 on: July 19, 2013, 01:29:57 pm »
Well that works perfectly for the 4 directions :D I have plenty more things I can be working on with the game, so refining the code is in no way a priority! Thanks so much for fixing all that!

 

anything