Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actuallride

Posted 06 January 2013 - 12:25 PM

To me your way should work because even though the animation stops when the up key is released, it should start again because the left key is being pressed.

But this is how I implemented SFML animation in my project

It's simple. Don't start the animation only if(sf::Event::keyIsPressed).   Start the animation always even though the key is not pressed and stop it later if key is not pressed

Below code is in the main loop

 

        animation.start(); //Just start the animation
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
           ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
             ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
               .....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                 .....
        else
            animation.stop(); //   -Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //   -Animation only gets updated when the animation has not been stopped


#5lride

Posted 06 January 2013 - 12:20 PM

This is how I implemented SFML animation in my project

It's simple. Don't start the animation only if(sf::Event::keyIsPressed).   Start the animation always even though the key is not pressed and stop it later if key is not pressed

Below code is in the main loop

 

        animation.start(); //Just start the animation
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
           ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
             ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
               .....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                 .....
        else
            animation.stop(); //   -Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //   -Animation only gets updated when the animation has not been stopped


#4lride

Posted 06 January 2013 - 12:14 PM

This is how I implemented SFML animation in my project

It's simple. Start the animation even though the key is not pressed and stop it later if key is not pressed

Below code is in the main loop

 

        animation.start(); //Just start the animation
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
           ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
             ....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
               .....
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                 .....
        else
            animation.stop(); //   -Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //   -Animation only gets updated when the animation has not been stopped


#3lride

Posted 06 January 2013 - 12:12 PM

This is how I implemented SFML animation in my project

It's simple. Start the animation even though the key is not pressed and stop it later if key is not pressed

Below code is in the main loop

 

        animation.start(); //Just start the animation
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(boundingRect.left+(-speed*dt)>104)
                move(-speed*dt, 0);
            animation.setY(1); //Face left
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(boundingRect.top+(-speed*dt)>8)
                move(0, -speed*dt);
            animation.setY(3); //Face up
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(boundingRect.left+boundingRect.width+speed*dt<920)
                move(speed*dt, 0);
            animation.setY(2);//face right
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(boundingRect.top+boundingRect.height+speed*dt<680)
                move(0, speed*dt);
            animation.setY(0);//face down
        }
        else
            animation.stop(); //   -Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //if(active)update....    -Animation only gets updated when the animation has not been stopped


#2lride

Posted 06 January 2013 - 12:10 PM

This is how I implemented SFML animation in my project

Have a 'active' bool variable and update the animation only if the animation is active.

Below code is in the main loop

 

        animation.start(); //animation::active=true;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(boundingRect.left+(-speed*dt)>104)
                move(-speed*dt, 0);
            animation.setY(1); //Face left
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(boundingRect.top+(-speed*dt)>8)
                move(0, -speed*dt);
            animation.setY(3); //Face up
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(boundingRect.left+boundingRect.width+speed*dt<920)
                move(speed*dt, 0);
            animation.setY(2);//face right
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(boundingRect.top+boundingRect.height+speed*dt<680)
                move(0, speed*dt);
            animation.setY(0);//face down
        }
        else
            animation.stop(); //animation::active=false;   -Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //if(active)update....    -Animation only gets updated when the animation has not been stopped


#1lride

Posted 06 January 2013 - 12:07 PM

This is how I implemented SFML animation in my project

Below code is in the main loop

 

        animation.start();
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(boundingRect.left+(-speed*dt)>104)
                move(-speed*dt, 0);
            animation.setY(1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(boundingRect.top+(-speed*dt)>8)
                move(0, -speed*dt);
            animation.setY(3);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(boundingRect.left+boundingRect.width+speed*dt<920)
                move(speed*dt, 0);
            animation.setY(2);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(boundingRect.top+boundingRect.height+speed*dt<680)
                move(0, speed*dt);
            animation.setY(0);
        }
        else
            animation.stop(); //Stop the animation when keys are not pressed

 

        ....

        animation.update(dt); //Animation only gets updated when the animation has not been stopped


PARTNERS