Delays in ActionScript (Flash)? Alternatives to setInterval
While setInterval is handy, it's kind of limiting. Every time I want to add a Tween I have to add a new method. I do not want to alter the structure of my 开发者_如何学JAVAcode just to add in some delays. Isn't there any way to say this without making methods for A and B?
// do A
// wait N seconds
// do B
I don't want to use a while
loop with Dates because I think it will be blocking. Isn't there anything like Thread.sleep
in ActionScript?
The Flash ActionScript Virtual Machine (AVM) is single-threaded. Any loop or sleep function would cause the whole SWF to freeze for the duration of the desired interval.
As far as I know, there is no equivalent to Thread.sleep
in AS3, and if there was, it would not do what you wanted for the reason outlined above.
Why don't you want to use functions?
You can use timers if you want more power and/or flexibility than setInterval
offers.
EDIT: You can define your functions inline (technically these are called closures); this sounds like it might be closer to what you want. For example:
setInterval(function () {
// code for A
setInterval(function () {
// code for B
}, 1000);
}, 1000);
If you're not against using methods, try using TweenLite. It allows you to play with all sorts of timings.
For example, after you finish an animation, you can call the following:
//call myFunction() after 2 seconds, passing 1 parameter: "myParam"
TweenLite.delayedCall(2, myFunction, ["myParam"]);
which could probably provide you with the delay you're looking for.
精彩评论