1 Reply Latest reply on May 13, 2009 5:31 AM by Ned Murphy

    Functions on Key Press

    Mattj0070
      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
        • 1. Re: Functions on Key Press
          Ned Murphy Adobe Community Professional & MVP

          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

          }