Sleeping in action script 2 using getTimer() method

98 views Asked by At

How can I correctly perform something like sleep function using getTimer()? I need to do an action every 15 seconds. The code below doesn't work. I compile it with mtasc compiler on Linux.

class Tuto
{
static var lastMsg = 0;
static var msgInt = 15000;

  static function main(mc)
  {

    if(getTimer() > lastMsg + msgInt)
      {
         trace("something");
         lastMsg = getTimer();
      }

  }

}
1

There are 1 answers

0
alebianco On BEST ANSWER

The main instruction will be executed just once. You have to build some kind of loop or rely on the tick events sent by the player to execute your code continuously.

The basic options are:

while (true) { doSomething() }

this will execute forever, but remember that the flashplayer is single threaded so while that runs everything else will be frozen, UI and user inputs included. this is only "good" if you are building some heavy-processing tool that has no need of interacting with the user.

setInterval(doSomething, 15000)

this creates an interval that will call your function every X milliseconds. This is the simplest option and probably what you're looking for.

addEventListener(Event.ENTER_FRAME, doSomething)

this registers a listener for the ENTER_FRAME event of the Flash Player, which will be dispatched 30 times per second (by default). Inside that function you can check the current time with getTimer() and decide if it's time to execute your logic.