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

Author Topic: switching frames  (Read 1534 times)

0 Members and 1 Guest are viewing this topic.

Morph

  • Newbie
  • *
  • Posts: 6
    • View Profile
switching frames
« on: May 25, 2013, 05:39:43 pm »
Hello there.
I have been having a bit of a problem that seems quite hard for me to handle in switching frames on key pressed buttons.
In the code, I have created an enum that has five commands "Up, Down,Left, Right, Attack". When the command attack is initiated, I cannot bring the sprite back to the way it was before attacking.
My code :

else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
      {
         source.y=Attack;
                        if(Event.type==sf::Event::KeyReleased && Event.key.code==sf::Keyboard::Space)
                                   source.y=Left;
      }

I cannot understand why it cannot check the condition there

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: switching frames
« Reply #1 on: May 25, 2013, 05:49:46 pm »
How could space be pressed and released at the same time?
Moreover, events work correctly only in the event loop.

Morph

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: switching frames
« Reply #2 on: May 25, 2013, 06:11:16 pm »
Well, it was an attempt to make the frame goes back to the way it was before attacking. :D

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: switching frames
« Reply #3 on: May 26, 2013, 02:38:16 am »
Sounds like you need to activate a timer when the guy attacks, and then, after a second or two, switch back to normal mode.

float attackTimer = 0;
bool isAttacking = false;
loop {
  // events etc
  if (space pressed && !isAttacking){
    isAttacking = true;
    attackTimer = 1;
    // switch sprite to attack mode
  }

  // game update
  if (isAttacking){
     attackTimer -= DT; // DT = duration of last frame
     if (attackTimer < 0){
       isAttacking = false;
       // switch sprite back to normal mode
     }
  }
}
 
« Last Edit: May 26, 2013, 10:29:01 am by Laurent »