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

Author Topic: Strange behavior while moving sprite  (Read 1147 times)

0 Members and 1 Guest are viewing this topic.

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Strange behavior while moving sprite
« 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);

 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Strange behavior while moving sprite
« Reply #1 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).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything