0

我正在尝试创建一个简单的计时器来运行一个函数。您可以在下面看到:

我想运行一段时间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();
}

但是上面的语句是行不通的。请告诉我我在这里缺少什么。

任何帮助都会很棒。

4

1 回答 1

1

您立即命令您的骰子在开始计时器的正下方停止。删除这些语句并将它们重新定位到throwDiceStop()函数中。此外,如果您使用flash.utils.setTimeout()设置一次性计时器会更好,因为在另一种情况下,您可能会错误地创建计时器或无法正确处理其停用(我看到您没有正确执行,顺便说一句) .

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
}
于 2013-08-27T13:24:28.480 回答