function foo() { lalala //yooo }
Chaining Greensock Tweens in As3
Chaining Greensock Tweens
The Greensock tweening library is very awesome, and it makes tweening movie clips through code very simple and easy to do. This is a more advanced topic on the subject so if you are not already familiar with green sock tweening check out Greensock Tweening Basics.
http://greensock.com/asdocs/
First, let’s take a look at a simple green sock tween. Let’s assume we have a display object on the screen called _ball. This snippet will tween the ball 100px to the right over a period of 1 second.
var newBallPosition:Number = _ball.x + 100;
TweenLite.to(_ball, 1, {x:newBallPosition});
In the snippet above the only parameter ( i.e. arguments in the curly braces) used was the x value. Suppose though that we want something to happen once this tween is finished. By adding another parameter, “onComplete”. This is an interesting parameter because it takes the name of the function to call after this tween is completed. Here is an example of how it might look.
TweenLite.to(_ball, 1, {x:newBallPosition, onComplete:ballFinishedMoving});
private function ballFinishedMoving():void
{
trace(“The ball has finished moving”);
}
This trace statement will be shown once the ball has finished moving from the tween. You can put another tween call inside of this handler function, and suddenly you have chained tweens! Woohoo!
You may find though that not being able to pass parameters to the onComplete function is a bit limiting. Well, fear not friends. There is a flexible and easy-to-use method for sending parameters! Now, stay with me here. We have another parameter for the TweenLite.to function, and that parameter is “onCompleteParams”. This takes an array and maps each element to an input for the onComplete function. If that explanation isn’t clear enough, let’s look at an example of this. Suppose we have three variables: a string called _chatacterName, a movie clip called _characterMC, and a sprite called _characterBullet. Let’s also assume we want to send all three of these variables to the onComplete function. It would look something like this:
var params:Array = [_characterName, _characterMc, _characterBullet];
TweenLite.to(_ball, 1, {x:newBallPosition, onComplete:ballFinishedMoving, onCompleteParams:params});
private function ballFinishedMoving(characterName:String, characterMc:MovieClip, characterBullet:Sprite):void
{
trace(“The ball has finished moving. Here are the params: ” characterName + “, “ + characterMc + “, “ + characterBullet);
}