SFML community forums

Help => General => Topic started by: MDK on March 01, 2017, 12:14:01 am

Title: Strange behavior while moving sprite
Post by: MDK on March 01, 2017, 12:14:01 am
When i'm moving sprite in right, up and down direction everything is ok. But when i'm moving left, sprite is moving about 2 times faster. I have repaired this by adding two additionall lines of code. But still i have no idea why this is happening. And should i add this 2 lines below every "if/else"? Or an error is somewhere else? This two additionall lines of code are:
                  heroSpeedVector.x = 0;
                  heroSpeedVector.y = 0;

And this is fragment of code responsible for moving.
     
            const float heroSpeed = 0.1;
            Vector2f heroSpeedVector;

            if(Keyboard::isKeyPressed(Keyboard::Right)){
               heroSpeedVector.x = heroSpeed;
                 guyRectSourceSprite.top = 60;
             }
             else heroSpeedVector.x = 0;
            heroS.move(heroSpeedVector);

              if(Keyboard::isKeyPressed(Keyboard::Left)){
               heroSpeedVector.x = -heroSpeed;
                guyRectSourceSprite.top = 120;
             }
             else heroSpeedVector.x = 0;
            heroS.move(heroSpeedVector);
                heroSpeedVector.x = 0;
                heroSpeedVector.y = 0;

              if(Keyboard::isKeyPressed(Keyboard::Up)){
             heroSpeedVector.y = -heroSpeed;
             guyRectSourceSprite.top = 180;
             }
             else heroSpeedVector.y = 0;
            heroS.move(heroSpeedVector);

             if(Keyboard::isKeyPressed(Keyboard::Down)){
            heroSpeedVector.y = heroSpeed;
             guyRectSourceSprite.top = 240;
             }
             else heroSpeedVector.y = 0;
            heroS.move(heroSpeedVector);

 
Title: Strange behavior while moving sprite
Post by: eXpl0it3r on March 01, 2017, 08:46:57 am
Don't you think it's a bit odd to have four calls to move()?
Just define what the move vector should be first and then call move once.
I also highly recommend to use proper indentation and line breaks, it will make it so much easier to read and understand the code.

As for the problem, I can't directly see it from the given code.

And finally, in a future step, you want to make your movement framerate independent, by multiplying the velocity with a delta time (frame time).