Skip to main content
Participant
May 13, 2009
Question

Functions on Key Press

  • May 13, 2009
  • 1 reply
  • 350 views
I am creating a driving game using the following method to drive the car forward:

if (Key.isDown(Key.UP)) {
speed += 1;
}

I also want to call a function when the key is pressed athough if I add a function call the function gets repeated as long as the key is pressed and I only require it to run once?

Can anyone help?

Cheers
This topic has been closed for replies.

1 reply

Ned Murphy
Legend
May 13, 2009

What you could do, is have a boolean variable that you use to control calling the functtion.  Let's say the variable is called allowFunction and it is initialized with a value of true ( var allowFunction = true; )...

if (Key.isDown(Key.UP)) {


     speed += 1;

     if(allowFunction){

          // call the function here

          allowFunction = false; // disables calling the function again for this period

     }

} else {

     allowFunction = true; // resets for next down time

}