views:

175

answers:

2

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 code 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?

+3  A: 

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);
Cameron
Thanks for that. It is not that I do not want to use functions. It's that I do not want to use functions for successive animations. I don't see why, "this thing flies in from the left, then this flies in from the right, then that text appears" need to be in separate functions. And while I can use delays with a Tweener, I'm looking to make my code more readable.
Yar
@yar: I agree this is a bit of a pain for such a simple scenario. I've updated my answer with a code example that might be closer to what you want to accomplish.
Cameron
@Cameron, yeah, I do Ruby now and we call them closures, but it seems to be that they're anonymous functions in Java :)... Anyway, this might work. I just have to check out what kind of access B can get to A's variables. Though it is so ugly :)...
Yar
@yar: Heh, nested closures are kinda ugly :-) Everything in the innermost scope can access the outer scopes' variables -- basically, B can access everything in A and outside A, while A can only access stuff outside A (and each scope can access its own stuff, of course).
Cameron
I'm marking this as best answer because if you DO have a series of sequential things to do, you really have to use methods (or closures), to ensure things get fired in the right order. That said, I might mention that while it's true that the Flash Engine is single threaded (and you cannot play with thread primitives), timers suffer from all problems of race conditions and that anyway...
Yar
+1  A: 

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.

teehoo
Thanks Teehoo, that's cool. We use Tweener all over the place, and i was using that: it also lets you use "onComplete" and that. Which is about the same as `setInterval`, no?
Yar