我正在尝试创建一个用于运行函数的简单Timer。你可以在下面看到:
我想暂时运行DiceOne
和DieTwo
动画。
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();
}
但上述陈述不起作用。请告诉我这里我缺少什么。
任何帮助都会很棒。
答案 0 :(得分:1)
您立即命令骰子在启动计时器下方停止。删除这些语句并将它们重新定位到throwDiceStop()
函数中。此外,如果您使用flash.utils.setTimeout()
来设置一次性计时器会更好,因为在另一种情况下,您可能会错误地创建计时器或无法正确处理其停用(我看到您不这样做)这是正确的,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
}