Flex Snippet - Simple Tween Effects

Skill

Flex Snippet - Simple Tween Effects

Posted in:

After a small discussion about actionscript tweening with one of the other coders here I decided to write up a small tutorial on some basic tweens. Now in the last Flex tutorial we used effects to enhance the visuals of our state transitions, but today we are going to use effects for something different all together. I am going to show how to use a couple of the many available effects to animate an object around the screen.

In the example below we have a small application that takes advantage of the move and resize effects (both extend TweenEffect). In the example you can click around on the main canvas to move the circle to where you clicked (it isn't perfect, but its close enough for gov't work), and you can also change the number in the bottom text box to control the size of the circle. The circle will of course animate (tween) to the new position and size. So go ahead and play around with that for a minute or two.

Get Adobe Flash player

Now its time to get to work. The first thing we are going to build is a class where we extend the Canvas component. In this class we will override the updateDisplayList function to use the graphics object of the canvas to draw a circle. We use a couple of simple drawing commands to do this. Below is the code for our circle.

package
{
  import mx.containers.Canvas;

  public class CircleNode extends Canvas
  {
    override protected function updateDisplayList(unscaledWidth:Number,
        unscaledHeight:Number):void
    {
      super.updateDisplayList(unscaledWidth, unscaledHeight);
      if(graphics)
        graphics.clear();
      graphics.beginFill(0x00FF23);
      graphics.lineStyle(2, 0x000000);
      graphics.drawCircle(width / 2, height / 2, width / 2);
    }
  }
}

The next thing we are going to do is build the basic interface for the application. Not much is included here - a panel with a canvas on which the circle will move, and a control bar with a label and text input. We are going to use the text input for defining the end move size.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="500" height="499" viewSourceURL="/files/TweenTutorial.mxml">
  <mx:Panel x="0" y="0" width="100%" height="100%" title="Tween Tutorial">
    <mx:Canvas id="mainCanvas" width="100%" height="100%" backgroundColor="#EFEFEF" />
    <mx:ControlBar>
      <mx:Label text="Ending Size:" />
      <mx:TextInput id="txtEndSize" width="50" text="100" />
    </mx:ControlBar>
  </mx:Panel>
</mx:Application>

Now to some scripting. First we are going to add a listener to the creationComplete event on the application. I called the function initApp. Then inside of a <Script> tag we add a couple of private variables and our initApp function. Inside the function we create our circle and initialize its position and size. This is also where we are introduced to the Move and Resize classes, which are effects that extend TweenEffect. These allow us to easily move and resize the circle. When we define a new Move or Resize we pass in the component that we want to tween. Our new application tag and the start of our script is below.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="500" height="499" creationComplete="initApp()"
   viewSourceURL="/files/TweenTutorial.mxml">

And the script.

<mx:Script>
 <![CDATA[
   import mx.effects.Resize;
   import mx.effects.Move;
   
   private var cn:CircleNode;
   private var mv:Move;
   private var rs:Resize;
   
   private function initApp():void
   {
     cn = new CircleNode();
     cn.width = 100;
     cn.height = 100;
     cn.x = 50;
     cn.y = 50;
     addChild(cn);
     
     mv = new Move(cn);
     rs = new Resize(cn);
   }
 ]]>
</mx:Script>

The last thing we need to do is add the function to start our tween effects. We are going to do this on the mouseUp event for the canvas. This code sets up the information for the move effect which includes the to and from x and y positions and how long to make the effect last (duration). We set the final x and y positions to the mouse positions. The resize event is setup the same way, but the final width and height is set from our text input in the control bar. After the setup we end the effect (in case it was already playing because of a different move) and start it again. Below are the last couple of pieces of code, the first of which is our new canvas and the second is the move function that goes into our script.

<mx:Canvas id="mainCanvas" width="100%" height="100%"
   backgroundColor="#EFEFEF" mouseUp="moveCircle(event)" />

Finally the actionscript.

private function moveCircle(event:MouseEvent):void
{
  mv.xFrom = cn.x;
  mv.yFrom = cn.y;
  mv.xTo = event.localX;
  mv.yTo = event.localY;
  mv.duration = 3000;
 
  rs.heightFrom = cn.height;
  rs.widthFrom = cn.width;
  rs.heightTo = Number(txtEndSize.text);
  rs.widthTo = Number(txtEndSize.text);
  rs.duration = 3000;
 
  mv.end();
  rs.end();
  mv.play();
  rs.play();
}

To finish up here are the full entire contents of the main mxml file.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="500" height="499" creationComplete="initApp()"
   viewSourceURL="/files/TweenTutorial.mxml">
  <mx:Script>
   <![CDATA[
     import mx.effects.Resize;
     import mx.effects.Move;
     
     private var cn:CircleNode;
     private var mv:Move;
     private var rs:Resize;
     
     private function initApp():void
     {
       cn = new CircleNode();
       cn.width = 100;
       cn.height = 100;
       cn.x = 50;
       cn.y = 50;
       addChild(cn);
       
       mv = new Move(cn);
       rs = new Resize(cn);
     }
     
     private function moveCircle(event:MouseEvent):void
     {
       mv.xFrom = cn.x;
       mv.yFrom = cn.y;
       mv.xTo = event.localX;
       mv.yTo = event.localY;
       mv.duration = 3000;
       
       rs.heightFrom = cn.height;
       rs.widthFrom = cn.width;
       rs.heightTo = Number(txtEndSize.text);
       rs.widthTo = Number(txtEndSize.text);
       rs.duration = 3000;
       
       mv.end();
       rs.end();
       mv.play();
       rs.play();
     }
   ]]>
 </mx:Script>
  <mx:Panel x="0" y="0" width="100%" height="100%" title="Tween Tutorial">
    <mx:Canvas id="mainCanvas" width="100%" height="100%"
       backgroundColor="#EFEFEF" mouseUp="moveCircle(event)" />
    <mx:ControlBar>
      <mx:Label text="Ending Size:" />
      <mx:TextInput id="txtEndSize" width="50" text="100" />
    </mx:ControlBar>
  </mx:Panel>
</mx:Application>

Hopefully this tutorial helps at least a few people out there. You can learn more about effects over at the Adobe livedocs site and look for a future tutorial on building custom tween effects. Leave a comment if you have any questions or problems.

Mark McDonnell
10/23/2007 - 10:28

I couldn't find any other area to post this request. Can you write up a tutorial on how to create a simple AS3 preloader in Flash CS3 Professional as I've only found one tutorial on Adobe's website and it was quite complex!?

Many thanks.

Kind regards,
Mark

reply

The Fattest
10/23/2007 - 11:26

Yeah I will try and get this done and out in the next day or two.

reply

Balaji
03/15/2008 - 22:23

this is great

reply

Paul
06/24/2008 - 14:43

Nice Work...I tried using the Flex Tween class...I'll really appreciate if you can help me with that.

Thanks

reply

Jack
12/26/2008 - 07:30

If you get time could you please quickly answer how to incorporate easing (elastic, regular, etc.) into motion.

Thanks.

reply

FOr3st
10/17/2009 - 11:24

       mv.easingFunction = Elastic.easeOut;

reply

FOr3st
10/17/2009 - 08:10

Thanks a lot!

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors