Jump to content

Delay on a Function Running?


0penS0urce
 Share

Recommended Posts

So, I have a game where the player repeatedly presses buttons in order from the screen. But, the user can cheat if they repeatedly press the buttons. So, How can I add a delay to a function, like a button press listener, so that it can't happen more than 2 times per second? Thanks For Your Help!!!

Link to comment
Share on other sites

There are many ways to do it, and if you post what your code is like currently, we'll be able to give more direct advice.

 

But you could simply do something with a timer, or a countdown:

var lastClickTime = game.time.elapsed;// ... awesome code herebutton = game.add.button(0, 0, 'button', onClick, this);// ... more awesome code hereonClick : function(e) {  var clickElapsedTime = game.time.elapsed - lastClickTime  if (clickElapsedTime >= 1000) {     // if at least one second has passed, perform action     doSomethingAwesome();     lastClickTime = game.time.elapsed;  }   }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...