Scripted Motion Tweens Tutorial

Penner Tweens, created by Robert Penner, use built-in Macromedia Flash MX 2004 classes to script tweens. This is the touch of class that adds to any flash application / navigation / presentation. To set it up, you define an ease type, then apply the ease type to a Tween object.

Create a new flash document (File -> New… -> Flash Document)

Double-Click ‘Layer 2’ and label it “A” (for Actions). We will place all actionscript on this layer.

Create a new layer (Insert -> Timeline -> Layer). Double-Click ‘Layer 1’ and label it “mc” (for movieclips).

Draw a box using the Rectangle Tool (hotkey is ‘R’) near the top left of the Stage.

Double-Click on your new box. Now Right-Click on your box and select “Convert to Symbol…”.

In the ‘Name’ field put “m_box”. In the ‘Behavior’ radio button group, select “Movie clip”. Press ‘OK’.

Having your m_box on the stage still selected, in the ‘Properties’ toolbar, where there is a white field that has “” enter “box_mc”. Notice that the instance name on the stage is different than the instance name on the stage. We use the “_mc” naming convention for actionscript hints in our code.

Next, select Frame ‘1’ in the ‘A’ Layer. Open up your actions panel (Window -> Development Panels -> Actions or using hotkey ‘F9’). Copy and paste this code into the ‘Actions’ panel.

var easeType = mx.transitions.easing.Regular.easeOut;
myTween = new mx.transitions.Tween(box_mc, "_y", easeType, 0, 300, 30);

Now you might be asking what this just did. Well lets take an indepth look at the code defintion of the Tween object.
(do not enter this code into your movie):

Tween(myMovie:MovieClip, myProperty:Property, easeType, startValue:Number, endValue:Number, frames(or seconds), secondsSet:Boolean);

myMovie:MovieClip — Targets the Instance of a Movieclip on the stage.
myProperty:Property — Selects the property of the Movieclip on the stage to tween. (I.E. _x, _width, _alpha, etc)
easeType as (Parent.Child) — Your tween definition set up as a variable. SetParent Types (Back, Bounce, Elastic, None, Regular, Strong). Child Types (easeIn, easeOut). (i.e. Bounce.easeOut).
startValue:Number — Starting value of property to tween. For more advanced tweens, try (box_mc._y) to get the current starting Y position of the targeted Movie Clip.
endValue:Number — Starting value of property to tween.
frames(or seconds) — Number of frames to tween over, or number of seconds to tween over.
secondsSet:Boolean — Optional value, defaults to false. When set to true, uses seconds instead of frames to tween over.

Save your document (File -> Save)

Test your movie (Control -> Test Movie or hotkey “Ctr-Enter”)

Your box should go from _y position 0 to 300. Now try out variations of properties and movieclips and tweens.

For smoother framerate, try changing the framerate to 31 fps. To do so, go to Modfiy -> Document… . In the Frame rate input box, type in ’31’. Press ‘OK’. Try your movie again.

To try out my click demo, replace your current code on you “A” Layer with this code:

someListener = new Object();
someListener.onMouseDown = function () {
var easeType = mx.transitions.easing.Elastic.easeOut;
myTweenY = new mx.transitions.Tween(box_mc, "_y", easeType, box_mc._y,_root._ymouse, 60);
myTweenX = new mx.transitions.Tween(box_mc, "_x", easeType, box_mc._x, _root._xmouse, 60);

For more easing variations, view the folder on your computer >>
..Program Files/Macromedia/Flash MX 2004/en/First Run/Classes/mx/transitions

Try out Rober Penner’s examples at:
Robert Penner’s Easing Demo