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();
}
}
}
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:
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.
this creates an interval that will call your function every X milliseconds. This is the simplest option and probably what you're looking for.
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.