I'm trying to create a simple Timer for running a function. You can see in below :
I would like to run DiceOne
and DieTwo
animations for a while.
rollBtn.addEventListener(MouseEvent.CLICK, rollBtnClicked);
function rollBtnClicked(evt:MouseEvent):void {
rollNum1 = rollDice();
rollNum2 = rollDice();
throwDice();
var myTimerStop:Timer = new Timer(2000); // 2 seconds
myTimerStop.addEventListener(TimerEvent.TIMER, throwDiceStop);
myTimerStop.start();
DiceOne.gotoAndStop(rollNum1);
DiceTwo.gotoAndStop(rollNum2);
}
function throwDice():void {
DiceOne.gotoAndPlay(0);
DiceTwo.gotoAndPlay(0);
}
function throwDiceStop(event:TimerEvent):void {
DiceOne.stop();
DiceTwo.stop();
}
But above statements won't work. Please tell me what am i missing here.
Any helps would be awesome.
You immediately order your dice to stop right below starting the timer. Remove these statements and relocate them into throwDiceStop()
function. Also, it'll be better if you use flash.utils.setTimeout()
to set a one-time timer, because in the other case you could mis-create a timer or fail to properly handle its deactivation (I see you don't do it properly, BTW).
function rollBtnClicked(evt:MouseEvent):void {
throwDice();
setTimeout(throwDiceStop,2000);
}
function throwDice():void {
DiceOne.play(); // don't get started with 0, as it'll end you up
// with consistent animation through different throws
DiceTwo.play();
}
function throwDiceStop():void {
rollNum1 = rollDice();
rollNum2 = rollDice();
DiceOne.gotoAndStop(rollNum1); // and only here select proper values for dice
DiceTwo.gotoAndStop(rollNum2);
// inform the game about the dice finally settling, TODO
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.