开发者

Delays in ActionScript (Flash)? Alternatives to setInterval

开发者 https://www.devze.com 2022-12-28 11:06 出处:网络
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 de

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消