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

Author Topic: Sprite does not move left or up  (Read 1079 times)

0 Members and 1 Guest are viewing this topic.

Theom

  • Newbie
  • *
  • Posts: 2
    • View Profile
Sprite does not move left or up
« on: October 10, 2011, 10:52:46 pm »
I tried both using SetPosition and Move but both had the same result, when I can move the sprite down and right, that is I can only add to the position not subtract.

The relevant code would be:
Code: [Select]

case sf::Event::KeyPressed:
                switch(Event.Key.Code)
                {
                case(sf::Keyboard::Up):
                    py-=2;
                case(sf::Keyboard::Down):
                    py+=2;
                case(sf::Keyboard::Left):
                    px-=2;
                case(sf::Keyboard::Right):
                    px+=2;
                }
...
                pyr.SetPosition(px, py);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite does not move left or up
« Reply #1 on: October 10, 2011, 10:57:23 pm »
You must add a "break" instruction at the end of each "case", otherwise the code goes through all of them.

Code: [Select]
               switch(Event.Key.Code)
                {
                case(sf::Keyboard::Up):
                    py-=2;
                    break;
                case(sf::Keyboard::Down):
                    py+=2;
                    break;
                case(sf::Keyboard::Left):
                    px-=2;
                    break;
                case(sf::Keyboard::Right):
                    px+=2;
                    break;
                }
Laurent Gomila - SFML developer

Theom

  • Newbie
  • *
  • Posts: 2
    • View Profile
Sprite does not move left or up
« Reply #2 on: October 10, 2011, 11:12:20 pm »
It worked. Thank you very much.

 

anything