I feel like this should be really simple, but I cannot figure out how to get a function to run at random intervals in AS3. Would someone mind showing me the most stripped-down version of a random interval timer?
Printable View
I feel like this should be really simple, but I cannot figure out how to get a function to run at random intervals in AS3. Would someone mind showing me the most stripped-down version of a random interval timer?
Code:package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends flash.display.Sprite
{
private var timer:Timer;
private var maxDelay:int = 2000;
public function Main():void
{
timer = new Timer(getRandDelay());
timer.addEventListener(TimerEvent.TIMER, doSomething);
timer.start();
}
private function getRandDelay():Number{
return Math.random()*maxDelay;
}
private function doSomething(evt:TimerEvent):void{
timer.delay = getRandDelay();
trace("bloop!, delay:"+timer.delay);
}
}
}
thank you, that's exactly what I needed!