
Copy link to clipboard
Copied
Hello, i would like to make a timer with a random delay in AS3.
For example, i click on a button and it trace "hello" in 2 sec. I click again and it trace "hello" in 5 sec. I click a third time, and it trace "hello" in 3 sec... etc... I tried to write this, to try to choose a delay between 2 sec and 8 sec :
var zedelais:Array = ["2000", "3000", "4000", "5000", "6000", "7000", "8000"];
var delaisok:Number = zedelais[Math.floor(Math.random()*zedelais.length)];
var zeTimer:Timer = new Timer(delaisok,1);
//var zeTimer:Timer = new Timer(5000,1);
azer.addEventListener(MouseEvent.CLICK, azergo);
function azergo(event:MouseEvent):void {
zeTimer.start();
}
zeTimer.addEventListener(TimerEvent.TIMER, timerFunction);
function timerFunction(event:TimerEvent):void{
trace("Hello world !!");
}
But it doesn't work like i want. The delay change only when the program is launch, not when i click on the button...
Any idea, please?
Thanks for your help
1 Correct answer
The reason it does not change is because you never tell it to. You set it when the program starts and never change it. Try adding the delay change in the click event handler function...
function azergo(event:MouseEvent):void {
zeTimer.delay = zedelais[Math.floor(Math.random()*zedelais.length)];
zeTimer.start();
}
Copy link to clipboard
Copied
The reason it does not change is because you never tell it to. You set it when the program starts and never change it. Try adding the delay change in the click event handler function...
function azergo(event:MouseEvent):void {
zeTimer.delay = zedelais[Math.floor(Math.random()*zedelais.length)];
zeTimer.start();
}

Copy link to clipboard
Copied
Yeah it works, thank you for your help
Copy link to clipboard
Copied
You're welcome

