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

Author Topic: Restricting Sprite Movement in Loop  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

GarryS

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.maximize-traffic.net
Restricting Sprite Movement in Loop
« on: January 06, 2012, 11:16:47 pm »
I am using SFML 2.0...

I am confident that there is a better way but I have been banging my head on the wall for hours here.  

Basically, I have a character (sprite) that needs to move in an array but only 1 time per key press.  The code shows up and down "movement".  Left and Right would need to be additionally coded.

Here is what I have cobbled together but I know that this can't be "right":

Code: [Select]


bool keyUp = false;
bool keyDown = false;

while (window.IsOpened()) {
  while (window.PollEvent(event)) {
    if ((keyUp == false)&& sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))) {
      keyUp = true;
      moveHero(dave.location,dave.location = dave.location -= X_TILES);
    }
    if ((keyUp == true)&&(!sf::Keyboard::IsKeyPressed(sf::Keyboard::Up)))
        keyUp = false;
      if ((keyDown == false)&&(sf::Keyboard::IsKeyPressed(sf::Keyboard::Down))) {
          keyDown = true;
          moveHero(dave.location,dave.location = dave.location += X_TILES);
      }
      if ((keyDown == true)&&(!sf::Keyboard::IsKeyPressed(sf::Keyboard::Down)))
          keyDown = false;
    }
  }
}


Anyone have a moment to help a noob out?

Thanks

GarryS

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.maximize-traffic.net
Resolved ( I think )
« Reply #1 on: January 07, 2012, 04:40:45 am »
This seems to be a much better way to accomplish what I was attempting.

Code: [Select]

   while (window.PollEvent(event)) {
      processEvents(window, event);
      switch (event.Type) {
        case sf::Event::KeyReleased:
        if (event.Key.Code == sf::Keyboard::Up) {
          moveHero(dave.location,dave.location = dave.location -= X_TILES);
          break;
        }
        if (event.Key.Code == sf::Keyboard::Down) {
          moveHero(dave.location,dave.location = dave.location += X_TILES);
          break;
        }
        if (event.Key.Code == sf::Keyboard::Left) {
          moveHero(dave.location,dave.location = dave.location -= 1);
          break;
        }
        if (event.Key.Code == sf::Keyboard::Right) {
          moveHero(dave.location,dave.location = dave.location += 1);
          break;
        }
        default:
        break;
      }
    }


Thanks

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Resolved ( I think )
« Reply #2 on: January 07, 2012, 04:49:06 am »
Code: [Select]

moveHero(dave.location,dave.location = dave.location -= X_TILES);


Don't do that. The C++ standards does not specify the order of evaluation for function arguments.

GarryS

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.maximize-traffic.net
Re: Resolved ( I think )
« Reply #3 on: January 07, 2012, 04:54:53 am »
Quote from: "Lee R"
Code: [Select]

moveHero(dave.location,dave.location = dave.location -= X_TILES);


Don't do that. The C++ standards does not specify the order of evaluation for function arguments.


Thanks for the info.  Fixing that now!

 

anything