PDA

View Full Version : randomized timer events...



juskin
November 25th, 2008, 02:51 PM
So I want to randomize a timer event for a game. I really would like it to select different times in an array, but first i am just trying to randomize it here is the code:

stop();

var clipATimer:Timer = new Timer(Math.random() * 5000);
clipATimer.addEventListener(TimerEvent.TIMER, clipAhit);
function clipAhit (e:TimerEvent):void {
gotoAndStop("windowAopen");
trace("timer fired");
}
clipATimer.start();

it seems to work, however after the first time it fires, it just keeps firing.


so this is what I thought would work, but Im sure the syntax is incorrect...
var timeArray = new Array("1500" , "2000" , "3000")

var clipATimer:Timer = new Timer(Math.random(timeArray) * 5000);
clipATimer.addEventListener(TimerEvent.TIMER, clipAhit);
function clipAhit (e:TimerEvent):void {
gotoAndStop("windowAopen");
trace("timer fired");
}
clipATimer.start();

creatify
November 25th, 2008, 04:21 PM
try something like this, it's not the most optimized, but you'll get the idea:


stop();
var cnt:int = 0;
var labels:Array = ["wA", "wB", "wC"];

var clipATimer:Timer = new Timer(Math.random() * 5000, 1);
clipATimer.addEventListener(TimerEvent.TIMER, clipAhit, false, 0, true);

function clipAhit (e:TimerEvent):void {
gotoAndStop(labels[cnt]);
cnt++;
if(cnt != labels.length) {
//restart timer
clipATimer = new Timer(Math.random() * 5000, 1);
clipATimer.addEventListener(TimerEvent.TIMER, clipAhit, false, 0, true);
clipATimer.start();
} else {
//dispose timer
clipATimer.removeEventListener(TimerEvent.TIMER, clipAhit);
clipATimer = null;
}
}
clipATimer.start();