在了解ActionScript 3 Timer类有难度有难度、ActionScript、Timer

2023-09-08 15:24:43 作者:梦想起航我起床

我试图做一个骰子游戏在Flash / ActioScript 3.我做了所有的必需品,它工作顺利进行。现在我想改善用户体验。举例来说,当它的计算机的转(滚,做根据模具价值的东西),我想模具动画。该芯片有6个关键帧。因此,对于,说,2秒模具将循环的6架则将在值停止(取决于随机生成)。不知怎的,我不能这样做,因为我想要的。我如何写一个函数(S),所以,当我说,

I'm trying to make a dice game in Flash/ActioScript 3. I did all the essentials and it works smoothly. Now I want to improve the user experience. For instance, when it's computer's turn (to roll and do things according to die value) I want to animate the die. The die has 6 keyframes. So, for, say, 2 seconds the die will loop those 6 frames then it will stop on a value (depending on random generator). Somehow I can't do it as I want. How can I write a function(s) so that when I say,

animateDice()

animateDice()

它会做什么,但只是动画骰子指定的时间间隔?

it will do nothing but just animate the dice for a specified interval?

更新:

var timer:Timer = new Timer(10, 50);
myButton.addEventListener(MouseEvent.CLICK, onClick);
timer.addEventListener(TimerEvent.TIMER, animateDice);

function onClick(event: Event):void {
    timer.start();
}

function animateDice(event: Event):void {
    dice.play();
}

例如,我不明白为什么上面的code不能正常工作。它在第一次点击正常工作,但不能有后。

For instance, I don't understand why the above code doesn't work properly. It does work properly on first click, but not there after.

更新2:我想我仍然有问题。我该如何暂停运行code,直到计时器停止? (是的,有一个变通---把定时器处理程序内的其他计时器等有没有一种简单的方法?

Update 2: I guess I'm still having problems. How do I suspend the running code until the timer stops? (Yes there is a work around---putting timer handlers inside other timers, etc. Is there an easy way?

也许,这将帮助:

首先,我们看到滚动的模具(和一个消息框,告知比赛将决定谁启动用户)。那么它的无论是人的或电脑的转变。当它的计算机的回合,首先我们看到的滚动再死,比如说,1秒。然后停止和我们看到的结局。我是个初学者,我的拒绝失去了一些东西,但是从我看到它似乎所有这些简单的步骤(只是显示滚动一段时间模具)意味着大量线条和大量的。

First we see the die rolling (and a message box informs the user that the game will decide whom starts). Then it's either Human's or Computer's turn. When it's computer's turn, first we see the rolling die again for, say, 1 second. Then it stops and and we see the outcome. I'm a beginner and I nay be missing something, but from what I see it seems that all these simple steps (just showing the die rolling for some time) means lots and lots of lines.

如果我使用的模具动画一个简单的定时器,剧本仍在继续,整个演出消失。

If I use a simple timer for die animation, the script continues and the whole show goes away.

推荐答案

定时器对象有三个属性:

The timer object has three properties:

延迟,或多久事件应激发 的repeatCount ,或多少次事件应激发 CURRENTCOUNT ,或多少次计时器的事件已解雇了迄今 delay, or how often the event should fire repeatCount, or how many times the event should fire currentCount, or how many times the timer's event has fired thus far

您正在创建与定时器新的定时器(10,50),这台延迟 10和的repeatCount 50。这意味着,一旦你调用 timer.start(),计时器会触发 TimerEvent.TIMER 每10毫秒。每次它被发射,它增加了1 CURRENTCOUNT 。当 CURRENTCOUNT 大于或等于的repeatCount (50),它停止循环定时器。

You are creating the timer with new Timer(10, 50), which sets delay to 10 and repeatCount to 50. This means that, once you call timer.start(), timer will fire TimerEvent.TIMER every 10 milliseconds. Each time it is fired, it adds 1 to currentCount. When currentCount is greater than or equal to repeatCount (50), it stops looping the timer.

在你的计时器已经停了,如果你调用 timer.start()再次,它只会触发事件一次,因为 CURRENTCOUNT 还没有被重置为零,并且仍然是>。=的repeatCount

Once your timer has stopped, if you call timer.start() again, it will only fire the event once, because currentCount has not been reset to zero, and is still >= repeatCount.

如果你调用 timer.reset()之前调用 timer.start(),它会设置这个值零和的东西应该像预期。

If you call timer.reset() before calling timer.start(), it will set this value to zero and things should behave as expected.