GreenSock


TweenMax (AS2) - TweenLite on Steriods

Posted in Tweening by jack on the April 5th, 2008


Version 1.15, Updated 5/7/2008


Download Now
 
Donate

Join Club GreenSock to get updates and a lot more

DESCRIPTION

TweenMax builds on top of the TweenLite core class and its big brother, TweenFilterLite, to round out the tweening family with popular (though not essential) features like bezier tweening, pause/resume capabilities, easier sequencing, hex color tweening, and more. TweenMax uses the same easy-to-learn syntax as its siblings. In fact, since it extends them, TweenMax can do anything TweenLite and/or TweenFilterLite can do, plus more. So why build 3 classes instead of one? Good question. The goal was to maximize efficiency and minimize file size. Frankly, TweenLite is probably all most developers will need for 90% of their projects, and it only takes up 3k. It's extremely efficient and compact considering its features. But if you need to tween filters or rich imaging effects like saturation, contrast, hue, colorization, etc., step up to TweenFilterLite at 6k (total). Still need more? No problem - snag TweenMax to add extra features jam-packed into 8k (total). See below for a chart describing the differences between the classes. TweenMax introduces an innovative feature called "bezierThrough" that allows you to define points through which you want the bezier curve to travel (instead of normal control points that simply attract the curve). Or use regular bezier curves - whichever you prefer. It'll even handle rotating your object so that it aligns with the direction of the bezier (orientToBezier)! See the sample below. Click to add bezier points.

Other links: AS3 Version | Speed Comparison | Bezier Speed Comparison

FEATURE COMPARISON

  TweenLite TweenFilterLite TweenMax
File size
3k 6k 8k
AS2 and AS3 versions with identical syntax
X X X
X X X
Tween ANY numeric property of any object
X X X
Tween multiple properties with a single line of code
X X X
Syncronized tweens
X X X
Use any standard easing equation
X X X
onComplete, onStart, and onUpdate callbacks, including the ability to pass any number of parameters to those functions (onCompleteParams, onStartParams, onUpdateParams)
X X X
Delay any tween by a set amount (good for sequencing)
X X X
Automatic garbage collection
X X X
Tween frames of a MovieClip
X X X
Tween tint of a MovieClip/DisplayObject
X X X
autoAlpha (toggles visibility of an object off when the alpha hits zero)
X X X
Tween the volume of any MovieClip/SoundChannel
X X X
Unique "from()" call that allows you to use the current properties as the end values
X X X
Use relative values
X X X
Tween arrays of numeric values with a single call
X X X
Automatically overwrite tweens of the same object by default in order to avoid conflicts (this behavior can easily be turned off too)
X X X
Pass extra parameters to easing equations (like Elastic.easeOut or Back.easeOut) for added control
X X X
Tween standard filters like Blur, Glow, DropShadow, Bevel, and ColorMatrix
  X X
Simple controls for tweening a MovieClip's/DisplayObject's saturation, hue, brightness, contrast, threshold, and colorization
  X X
Bezier tweening
    X
Bezier "through" tweening (define points to hit instead of just control points)
    X
Automatically orient an object's rotation to a Bezier path
    X
Pause/Resume capability
    X
Easier sequencing
    X
Tween multiple objects with a single call, including a special "delayIncrement" property that staggers the starting time of each tween
    X
isTweening() function for easily determining if an object is currently tweening
    X
Jump to any point in the tween using the "progress" property
    X
Tween any number of hex color properties
    X
Get an array of all TweenMax (and TweenLite and TweenFilterLite) instances that are currently affecting a particular target object.
    X
Price:
FREE (supported by donations)

USAGE

TweenMax.to(target:Object, duration:Number, variables:Object):TweenMax

  • Description: Tweens the target's properties from whatever they are at the time you call the method to whatever you define in the variables parameter.
  • Parameters:
    1. target : Object - Target object whose properties we're tweening
    2. duration : Number - Duration (in seconds) of the tween
    3. variables : Object - An object containing the end values of all the properties you'd like to have tweened (or if you're using the TweenMax.from() method, these variables would define the BEGINNING values).
      Special Properties:

      • delay : Number - The number of seconds you'd like to delay before the tween begins. This is very useful when sequencing tweens
      • ease : Function - You can specify a function to use for the easing with this variable. For example, mx.transitions.easing.Elastic.easeOut. The Default is Regular.easeOut.
      • easeParams : Array - An array of extra parameter values to feed the easing equation. This can be useful when you use an equation like Elastic and want to control extra parameters like the amplitude and period. Most easing equations, however, don't require extra parameters so you won't need to pass in any easeParams.
      • autoAlpha : Number - Same as changing the "alpha" property but with the additional feature of toggling the "visible" property to false if the alpha ends at 0. It will also toggle visible to true before the tween starts if the value of autoAlpha is greater than zero.
      • volume : Number - To change a MovieClip's volume, just set this to the value you'd like the MovieClip to end up at (or begin at if you're using TweenMax.from()).
      • tint : Number - To change a MovieClip's color, set this to the hex value of the color you'd like the MovieClip to end up at(or begin at if you're using TweenMax.from()). An example hex value would be 0xFF0000. If you'd like to remove the color from a MovieClip, just pass null as the value of tint. Before version 5.8, tint was called mcColor (which is now deprecated and will likely be removed at a later date although it still works)
      • frame : Number - Use this to tween a MovieClip to a particular frame.
      • bezier : Array - Bezier tweening allows you to tween in a non-linear way. For example, you may want to tween a MovieClip's position from the origin (0,0) 500 pixels to the right (500,0) but curve downwards through the middle of the tween. Simply pass as many objects in the bezier array as you'd like, one for each "control point" (see documentation on Flash's curveTo() drawing method for more about how control points work). In this example, let's say the control point would be at x/y coordinates 250,50. Just make sure your my_mc is at coordinates 0,0 and then do: TweenMax.to(my_mc, 3, {_x:500, _y:0, bezier:[{_x:250, _y:50}]});
      • bezierThrough : Array - Identical to bezier except that instead of passing bezier control point values, you pass points through which the bezier values should move. This can be more intuitive than using control points.
      • orientToBezier : Array (or Boolean) - A common effect that designers/developers want is for a MovieClip/Sprite to orient itself in the direction of a Bezier path (alter its _rotation). orientToBezier makes it easy. In order to alter a rotation property accurately, TweenMax needs 4 pieces of information:
        1. Position property 1 (typically "_x")
        2. Position property 2 (typically "_y")
        3. Rotational property (typically "_rotation")
        4. Number of degrees to add (optional - makes it easy to orient your MovieClip/Sprite properly)

        The orientToBezier property should be an Array containing one Array for each set of these values. For maximum flexibility, you can pass in any number of arrays inside the container array, one for each rotational property. This can be convenient when working in 3D because you can rotate on multiple axis. If you're doing a standard 2D x/y tween on a bezier, you can simply pass in a boolean value of true and TweenMax will use a typical setup, [["_x", "_y", "_rotation", 0]]. Hint: Don't forget the container Array (notice the double outer brackets)

      • hexColors : Object - Although hex colors are technically numbers, if you try to tween them conventionally, you'll notice that they don't tween smoothly. To tween them properly, the red, green, and blue components must be extracted and tweened independently. TweenMax makes it easy. To tween a property of your object that's a hex color to another hex color, use this special hexColors property of TweenMax. It must be an OBJECT with properties named the same as your object's hex color properties. For example, if your my_obj object has a "myHexColor" property that you'd like to tween to red (0xFF0000) over the course of 2 seconds, do: TweenMax.to(my_obj, 2, {hexColors:{myHexColor:0xFF0000}}); You can pass in any number of hexColor properties.
      • onStart : Function - If you'd like to call a function as soon as the tween begins, pass in a reference to it here. This can be useful when there's a delay and you want something to happen just as the tween begins.
      • onStartParams : Array - An array of parameters to pass the onStart function.
      • onStartScope : Object - Use this to define the scope of the onStart function.
      • onUpdate : Function - If you'd like to call a function every time the property values are updated (on every frame during the time the tween is active), pass a reference to it here.
      • onUpdateParams : Array - An array of parameters to pass the onUpdate function (this is optional)
      • onUpdateScope : Object - Use this to define the scope of the onUpdate function.
      • onComplete : Function - If you'd like to call a function when the tween has finished, use this.
      • onCompleteParams : Array - An array of parameters to pass the onComplete function (this is optional)
      • onCompleteScope : Object - Use this to define the scope of the onComplete function.
      • renderOnStart : Boolean - If you're using TweenMax.from() with a delay and want to prevent the tween from rendering until it actually begins, set this special property to true. By default, it's false which causes TweenMax.from() to render its values immediately, even before the delay has expired.
      • overwrite : Boolean - If you do NOT want the tween to automatically overwrite any other tweens that are affecting the same target, make sure this value is false.
      • blurFilter : Object - To apply a BlurFilter, pass an object with one or more of the following properties:
        • blurX
        • blurY
        • quality
      • glowFilter : Object - To apply a GlowFilter, pass an object with one or more of the following properties:
        • alpha
        • blurX
        • blurY
        • color
        • strength
        • quality
        • inner
        • knockout
      • colorMatrixFilter : Object - To apply a ColorMatrixFilter, pass an object with one or more of the following properties:
        • colorize
        • amount
        • contrast
        • brightness
        • saturation
        • hue
        • threshold
        • relative
        • matrix
      • dropShadowFilter : Object - To apply a DropShadowFilter, pass an object with one or more of the following properties:
        • alpha
        • angle
        • blurX
        • blurY
        • color
        • distance
        • strength
        • quality
      • bevelFilter : Object - To apply a BevelFilter, pass an object with one or more of the following properties:
        • angle
        • blurX
        • blurY
        • distance
        • highlightAlpha
        • highlightColor
        • shadowAlpha
        • shadowColor
        • strength
        • quality



TweenMax.from(target:Object, duration:Number, variables:Object):TweenMax

  • Description: Exactly the same as TweenMax.to(), but instead of tweening the properties from where they're at currently to whatever you define, this tweens them the opposite way - from where you define TO where ever they are now (when the method is called). This is handy for when things are set up on the stage the way the should end up and you just want to tween them to where they are.
  • Parameters: Same as TweenMax.to(). (see above)



TweenMax.sequence(target:Object, tweenObjects:Array):Array

  • Description: Provides an easy way to sequence a set of tweens, one after the other. It's essentially the same thing as making a bunch of individual TweenMax.to() calls on the object with overwrite set to false, and the delays stacked.
  • Parameters:
    1. target : Object - The object whose properties to tween.
    2. tweenObjects : Array - An array of tween objects. IMPORTANT: each object must contain a "time" property defining the duration of that tween. Example: TweenMax.sequence(mc, [{time:1, _x:"200"}, {time:2, autoAlpha:0, onComplete:myFunction}]);



TweenMax.allTo(targets:Array, duration:Number, vars:Object):Array

  • Description: Provides an easy way to tween multiple objects to the same values. It also accepts a few special properties, one of which is "delayIncrement" which staggers the start time of each tween. For example, you might want to have 5 MovieClips move down 100 pixels while fading out, and stagger the start times slightly by 0.2 seconds, you could do:
    TweenMax.allTo([mc1, mc2, mc3, mc4, mc5], 1, {_y:"100", _alpha:0, delayIncrement:0.2});
  • Parameters:
    1. targets : Array - An array of objects to tween.
    2. duration : Number - Duration (in seconds) of the tween
    3. vars : Object - An object containing the end values of all the properties you'd like to have tweened (or if you're using the TweenMax.allFrom() method, these variables would define the BEGINNING values).
      Special Properties:

      • IMPORTANT: Accepts the exact same special properties as the to() and from() methods (see list above), and adds these:
      • delayIncrement : Number - delay (in seconds) between each tween's start time. This is useful for staggering the tweens.
      • onCompleteAll : Function - If you'd like to call a function when ALL of the tweens have finished, use this.
      • onCompleteAllParams : Array - An array of parameters to pass the onCompleteAll function
      • onCompleteAllScope : Array - Use this to define the scope of your onCompleteAll function



TweenMax.allFrom(targets:Array, duration:Number, vars:Object):Array

  • Description: Exactly the same as TweenMax.allTo(), but instead of tweening the properties from where they're at currently to whatever you define, this tweens them the opposite way - from where you define TO where ever they are. This is handy for when things are set up on the stage the way they should end up and you just want to tween them into place.
  • Parameters: Same as TweenMax.allTo(). (see above)



pause():Void

  • Description: Pauses the tween. When a tween is paused, it's "paused" property is set to true.



resume():Void

  • Description: Resumes a paused the tween. When a tween is resumed, it's "paused" property is set to false.



TweenMax.getTweensOf(target:Object):Array

  • Description: Gets an array of all tweens affecting the target object.
  • Parameters:
    1. target : Object - The object whose tweens you want to get.



TweenMax.isTweening(target:Object):Boolean

  • Description: Checks to see if a particular object is currently being tweened (paused tweens don't count).
  • Parameters:
    1. target : Object - The object whose tweens you want to check.



TweenMax.getAllTweens():Array

  • Description: Returns an array of all tweens (including paused tweens).



TweenMax.delayedCall(delay:Number, onComplete:Function, onCompleteParams:Array, scope:Object);

  • Description: Provides an easy way to call any function after a specified number of seconds. Any number of parameters can be passed to that function when it's called too.
  • Parameters:
    1. delay : Number - Number of seconds before the function should be called.
    2. onComplete : Function - The function to call
    3. onCompleteParams : Array - [optional] An array of parameters to pass the onComplete function when it's called.
    4. scope : Object - [optional] Defines the scope of the function.



TweenMax.killTweensOf(target:Object, complete:Boolean);

  • Description: Provides an easy way to kill all tweens of a particular Object/MovieClip. You can optionally force it to immediately complete (which will also call the onComplete function if you defined one)
  • Parameters:
    1. target : Object - Any/All tweens of this Object/MovieClip will be killed.
    2. complete : Boolean - If true, the tweens for this object will immediately complete (go to the ending values and call the onComplete function if you defined one).




TweenMax.killDelayedCallsTo(function:Function);

  • Description: Provides an easy way to kill all delayed calls to a particular function (ones that were instantiated using the TweenMax.delayedCall() method).
  • Parameters:
    1. function : Function - Any/All delayed calls to this function will be killed.



TweenMax.killAllTweens(complete:Boolean)

  • Description: Kills ALL tweens (but not delayedCalls).
  • Parameters:
    1. complete: Boolean - If you'd like to force the tweens to complete, set this to true. (it's false by default)



TweenMax.killAllDelayedCalls(complete:Boolean):Void

  • Description: Kills ALL delayedCalls.
  • Parameters:
    1. complete: Boolean - If you'd like to force the delayedCall to complete, set this to true. (it's false by default)



TweenMax.pauseAll(tweens:Boolean, delayedCalls:Boolean):Void

  • Description: Provides a way to pause all TweenMax tweens and/or delayedCalls (not TweenLite/TweenFilterLite tweens or delayedCalls).
  • Parameters:
    1. tweens: To pause tweens, set this to true. (it is true by default)
    2. delayedCalls: To pause delayedCalls, set this to true. (it is false by default)



TweenMax.resumeAll(tweens:Boolean, delayedCalls:Boolean):Void

  • Description: Resumes all TweenMax tweens
  • Parameters:
    1. tweens: To resume tweens, set this to true. (it is true by default)
    2. delayedCalls: To resume delayedCalls, set this to true. (it is false by default)



TweenMax.removeTween(tween:TweenLite):Void

  • Description: Kills a single tween instance.
  • Parameters:
    1. tween: The TweenMax/TweenFilterLite/TweenLite instance that you'd like to kill.


EXAMPLES

To set up a sequence where we fade a MovieClip to 50% opacity over the course of 2 seconds, and then slide it down to _y coordinate 300 over the course of 1 second:

  1. import gs.TweenMax;
  2. TweenMax.sequence(clip_mc, [{time:2, _alpha:50}, {time:1, _y:300}]);


To tween the clip_mc MovieClip over 5 seconds, changing the _alpha to 50%, the _x to 120 using the Back.easeOut easing function, delay starting the whole tween by 2 seconds, and then call a function named "onFinishTween" when it has completed and pass in a few parameters to that function (a value of 5 and a reference to the clip_mc), you'd do so like:

  1. import gs.TweenMax;
  2. import mx.transitions.easing.Back;
  3. TweenMax.to(clip_mc, 5, {_alpha:50, _x:120, ease:Back.easeOut, delay:2, onComplete:onFinishTween, onCompleteParams:[5, clip_mc]});
  4. function onFinishTween(argument1:Number, argument2:MovieClip):Void {
  5.     trace("The tween has finished! argument1 = " + argument1 + ", and argument2 = " + argument2);
  6. }

If you have a MovieClip on the stage that is already in it's end position and you just want to animate it into place over 5 seconds (drop it into place by changing its _y property to 100 pixels higher on the screen and dropping it from there), you could:

  1. import gs.TweenMax;
  2. import mx.transitions.easing.Elastic;
  3. TweenMax.from(clip_mc, 5, {_y:"-100", ease:Elastic.easeOut});

FAQ

  1. How do I install the class? Do I have to import it on every frame?
    Just make sure the "gs" folder from the download is in the same folder as your FLA file. Keep the class files in the "gs" folder (don't remove them). That's it. And, yes, just like any Class, you need to import TweenMax at the top of any frame that contains code referencing it. This does NOT add extra Kb to your file size very time you import it. Flash is smart enough to embed it once and all the other import statments just act as a "pointer" to the embedded Class.

  2. Why doesn't the tweening engine selectively overwrite ONLY tweens that are affecting the same property? How can I achieve this effect?
    As you can tell from the speed tests, performance is a primary concern. Searching through potentially hundreds or thousands of tweens for overlapping tween properties can get expensive in terms of performance. Therefore, the TweenLite family opted for a much faster approach: all or nothing. But don't worry - in almost every case, you can achieve the result you're after with a little bit of extra code. Trust me, it's worth the increased speed and efficiency you'll get.

    You can selectively kill tweens by keeping track of the TweenMax instance that’s returned by the to() or from() methods, and calling removeTween() on them. For example, if you want to have a button scale up when you roll over it, and scale back down when you roll off (those tweens should overwrite each other) but you don't want them to overwrite a from() call that fades the button in over the course of 2 seconds, you could do:

    1. TweenMax.from(myButton, 2, {_alpha:0, overwrite:false});
    2. var scaleTween:TweenMax;
    3. myButton.onRollOver = function():Void {
    4.     TweenMax.removeTween(scaleTween);
    5.     scaleTween = TweenMax.to(myButton, 0.5, {_xscale:120, _yscale:120, overwrite:false});
    6. }
    7. myButton.onRollOut = function():Void {
    8.     TweenMax.removeTween(scaleTween);
    9.     scaleTween = TweenMax.to(myButton, 0.5, {_xscale:100, _yscale:100, overwrite:false});
    10. }

    Remember, other tweens of the same object will always be overwritten by default. You must set overwrite:false to avoid that behavior. The above example is essentially allowing you to selectively handle overwriting yourself instead of relying on the tweening engine to do the "all or nothing" technique.

  3. How do I pause/resume a tween?
    Simple. Whenever you do a to() or from() tween, the function returns a reference to that particular tween, so just keep track of that and call pause() or resume() on it, like this:

    1. import gs.TweenMax;
    2. var myTween = TweenMax.to(mc, 2, {colorMatrixFilter:{colorize:0xFF0000, amount:1}});
    3. myTween.pause();
    4. myTween.resume();

  4. How do I get/set the progress of a tween?
    Just use the "progress" property. The value should always be between 0 and 1 (0 = tween is at its starting values, 0.5 = half-way finished, and 1 = the tweening is complete) Whenever you do a to() or from() tween, the function returns a reference to that particular tween, so just keep track of that and call get/set the progress property, like this:

    1. import gs.TweenMax;
    2. var myTween = TweenMax.to(mc, 2, {colorMatrixFilter:{colorize:0xFF0000, amount:1}});
    3. myTween.progress = 0.75;

  5. Why aren't my filters working?
    If you're using a filter that has an alpha property, try setting it to 1. The default alpha value is zero, so the filter may be working just fine, but you're not seeing it.

  6. Do the properties have to be in a specific order?
    Nope. The only thing that matters is that the first parameter is the object you're tweening, the second parameter is the time (in seconds), and the third parameter contains all the properties you want to tween (in any order). So TweenMax.to(clip_mc, 1, {colorMatrixFilter:{colorize:0xFF0000, amount:1}}) is the same as TweenMax.to(clip_mc, 1, {colorMatrixFilter:{amount:1, colorize:0xFF0000}});

  7. Do I have to purchase a license to use this code? Can I use it for commercial purposes?
    TweenMax is free. You're welcome to use it for commercial purposes - I only ask that you donate what you think the class is worth in your project(s), and consider joining Club GreenSock which gives you some bonus classes, updates, and more. If you want to use one (or more) of my classes in your commerical software product which would entail distributing the class files, please get my permission first by e-mailing me at jack -at- greensock.com.

Need Help?

Feel free to e-mail me a question. I'd highly recommend joining Club GreenSock to get prioritized access to my time in answering your question, and so that you receive updates and lots more. When you e-mail your question, please include a simplified FLA file (and any class files) that clearly demonstrates the problem and provide a brief explanation.

Author: Jack Doyle, (e-mail: jack -at- greensock.com)
Copyright 2008, GreenSock (This work is subject to the terms here.)

6 Responses to 'TweenMax (AS2) - TweenLite on Steriods'

Subscribe to comments with RSS

  1. Luis Grolez said,

    on April 6th, 2008 at 2:42 am

    Cool TweenMax, I was waiting for this class for a long time,
    I really like the way I be able to control curves,
    Congrats to bring it out,
    Luis

  2. Iulian said,

    on April 7th, 2008 at 6:23 am

    When using TwenMax.from() with a delay, take a look at this code:

    TweenMax.from(mc1, 2, {blurFilter:{blurX:50, quality:2}, ease:Strong.easeOut, overwrite:false, delay:2});

    The blurFilter is applied from start, it doesn’t wait for the delay.

  3. jack said,

    on April 7th, 2008 at 10:15 am

    Actually, Iulian, that’s exactly the way it’s supposed to work. The from() method will apply the values immediately UNLESS you pass in “renderOnStart:true”. This is because usually when people use the from() method, they WANT the values to be applied immediately. For example, I’ll often have stuff build onto the screen in a staggered way, so I’ll put them at their end positions on the stage, then use a bunch of from() calls to make them fly/fade into place, and I’ll use delays. But if the values only got applied when the delay expired, you’d see a bunch of stuff on the screen initially, then they’d disappear and fly/fade on - see what I mean? But if that’s what you want, the renderOnStart:true is precisely what you need.

  4. Dario said,

    on April 11th, 2008 at 7:08 am

    Awesome! I was waiting for something like this even before the internet was invented! I used Fuse before this mainly because of it’s chain animation feature. But now with TweenMax, goodbye Fuse forever!

  5. Claes said,

    on April 15th, 2008 at 6:26 pm

    You made my day!
    Thankyouthankyouthankyou

  6. FeRcHiTo said,

    on April 30th, 2008 at 5:48 pm

    OUTSTANDING !!! today u wrote a new page on my life as flash designer! speechless.

Leave a Reply