views:

575

answers:

4

Here's what I want to do in code. Animate an MC of a car for a duration of x seconds. After the car is done, move an MC of a dog for y seconds. And so on...

With this code, all animations execute simultaneously.

car.slideTo(200,100,1);
dog.slideTo(200,100,5);
blimp.slideTo(200,100,2);

...

Is it possible to write a pause function to put in between those lines? I've tried using getTimeout, but I want to avoid having to define each animation as a separate function.

A: 

You can try using setInterval(my_func, 1000), which calls my_func after a 1 second.

cloudhead
+2  A: 

Hi, I strongly advice you to use TweenMax http://blog.greensock.com/tweenmaxas3/

It has all the animation tweening functions that you can dream of, including pausing in the middle of code.

It's also super easy to use, e.g.

import gs.TweenMax;
// move the movieclip to (500,200) in 2 seconds:
var myTween:TweenMax = new TweenMax(mc, 2, {x:500, y:200});

and you can pause it anytime like

myTween.pause();

But in your case you probably want to queue up your tweens

So you can write like

import gs.*;

var tween1:TweenMax = new TweenMax(mc1, 1, {x:300,y:400});
var tween2:TweenMax = new TweenMax(mc2, 1, {x:200,y:400});
var tween3:TweenMax = new TweenMax(mc3, 1, {x:100,y:400});

var myGroup:TweenGroup = new TweenGroup([tween1, tween2, tween3]);
myGroup.align = TweenGroup.ALIGN_SEQUENCE;

Also, TweenMax & Tweensy are currently the fastest tweening libraries existed in AS3, but TweenMax is a bit easier to use in my opinion, comparison between different tweening engines here http://blog.greensock.com/tweening-speed-test/

Unreality
This doesn't address the user's question at all.
fenomas
@fenomas: you don't know what you are saying. He wants to pause the animation tweening, I offer him the library to do that.
Unreality
No, he wants one animation to start when the previous animation stops. Regardless of whether he uses your library, or any library, to do the tweening, he'll still need either a callback or a timeout/interval. The library you suggest doesn't make it any easier.
fenomas
My library make it EASIER. The animation tweening library have much easier to use timeout/interval functions. Read the api of TweenMax first!
Unreality
I'm sure it's a lovely tween, but the questioner asked for a way to delay his tweens without making a callback function. If TweenMax does that, update your answer to mention that, and then you'll be answering his question. And if the questioner thinks it's worth adding 7kb or whatever to his file to avoid defining a function, he can do that.
fenomas
hey TweenMax doesn't need the callback function. The question: "Here's what I want to do in code. Animate an MC of a car for a duration of x seconds. After the car is done, move an MC of a dog for y seconds. And so on..." And you know TweenMax can queue up the tweenings? It's soooooooooooo easy to do that. :)
Unreality
A: 

You might want to use SetInterval().

Here is a simple example that might help:

var Timer = setInterval(delay, 500); //calls the function delay after 500 milliseconds

function delay () {
    trace("delayed!");
    clearInterval(Timer); //stops the function from being called again
}
Colin
A: 

Your question more or less exposes a misunderstanding of how ActionScript works. When Flash is executing your contents, what it does is: first it runs all your frame scripts, then it advances your animations by one frame, then it updates the screen. Then it runs all the scripts on the next frame, then it advances the animations again, then it updates the screens.

So if you put in some kind of pause function between two lines of code, Flash would do nothing at all during the pause, because it's still trying to finish up that set of frame scripts. Then once the pauses were finished, then Flash would finally update the screen, so all three animations would still work at the same time.

It might seem like a pain, but getTimeout is what you should be using. Or, you can also use a tween library that includes a feature to delay the start of the tween, such as the one presented in Unreality's answer. Underneath it amounts to the same thing.

fenomas
read his question man, he don't want the animations execute simultaneously, this is why TweenMax comes into handy.
Unreality
My answer is explaining why pausing AS execution would not give him the effect he wants. I'm sure your tween library is lovely, but he should still use a timeout/interval to sync the tweens, no matter what library he uses.
fenomas
No he shouldn't use timeout/interval to sync the tweens if there is a much better and easier library to use. TweenMax is one of the best in terms of tweening performance. You only have very slight chance of writing a better performance function than TweenMax. I can guarantee the library's timeout/interval function will be much more efficient than you write by yourself
Unreality
My point is that the questioner asked for a way to pause the execution of AS, so I'm explaining why that wouldn't solve his problem, so that he'll have a clearer picture of what his problem is. Whether he then wants to solve the problem with your library is then for him to decide. He may be happy with the library he's already using. I added an update, though, to make you happy. ;)
fenomas
thx for the happiness. Now, how can I make you happy in return? ;)
Unreality
Edit your answer? :) I only glanced at the tweenmax API, but the code you posted appears to make a tween that lasts 2 seconds, not a tween that is delayed 2 seconds. I think you need to do something like: new TweenMax(mc, 2, {x:500, y:200, delay:2});
fenomas
well TweenGroup is a better choice for sequencing. I've updated my answer, to make you happy in return :D
Unreality