<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2009://34.35576-</id>
  <updated>2009-11-16T15:12:02Z</updated>
  <title>Comments for Using Tweener for Scripted Animations (http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.35576</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=35576" title="Using Tweener for Scripted Animations" />
    <published>2009-03-11T21:46:53Z</published>
    <updated>2009-03-12T16:43:34Z</updated>
    <title>Using Tweener for Scripted Animations</title>
    <summary>Whenever adding effects to Flex applications, I usually user the built-in Flex effects.  They work with most flex components, are consistent, and are reliable.  However, in cases where you want your application to be very expressive, they don&apos;t always act like you want them to.  In those cases, I use Tweener, an animation &quot;tween&quot; library for ActionScript.   In this post, we&apos;ll walk through a basic example using Tweener.</summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[Whenever adding effects to Flex applications, I usually user the built-in Flex effects.  They work with most flex components, are consistent, and are reliable.  However, in cases where you want your application to be very expressive, they don't always act like you want them to.  In those cases, I use <a href="
http://code.google.com/p/tweener/" target="_blank">Tweener</a>, an animation "tween" library for ActionScript.   In this post, we'll walk through a basic example using Tweener.

<br/><br/>

Before getting into the guts of how to use Tweener, let's examine something that it can do.  Below is a Flex application that displays 25 colored circles in random postiions that animate whenever you click on the movie clip.   The size, x, y, and alpha/opacity transitions are controlled by the Tweener library.   

<br/><br/>

<i>Click on the circles to randomize and animate their positions</i><br/>
<iframe src="http://www.tricedesigns.com/portfolio/tweener/" width="550" height="450" frameborder="0"></iframe>

<br/>

The Tweener library does not require the Flex framework; It is an ActionScript library that can be used in Flex or Flash projects.  There are official versions of it ActionScript 2 and ActionScript 3, and are even unofficial ports for Java, JavaScript, HaXe, etc...   In this example, I chose to use a Flex application to show how easy it is to integrate with Flex.   However, since I am not using any Flex-specific controls (Buttons, Containers, Lists, Grids, Charts, etc...), I could have done the same thing with an ActionScript only project, with a much smaller file size.   Although I'm not using Flex-specific controls beyond <code>UIComponent</code>, all of these techniques can be used on any of the Flex components.

<br/><br/>

Getting started with Tweener is easy.  Just download the Tweener.swc file from the <a target="_blank" href="http://code.google.com/p/tweener/">Tweener home page</a>, and drop it into the "lib" directory in your Flex project.   Any .swc library files that are in this folder will automatically be added to your Flex project, without the need to alter any project properties. 

<br/><br/>

First, let's examine the visual piece of the application.   The <code>CircleRenderer</code> class is a visual component the extends the UIComponent class.   Basically, it just draws a circle based on the <code>size</code> and <code>color</code> values, with the center of the circle being at the registration point <code>(0, 0)</code> of the UIComponent instance.  It uses the normal Flex component lifecycle method of overriding the the <code>updateDisplayList</code> function to only render the circle if the display list has been invalidated.   Each time that the <code>size</code> or <code>color</code> values is changed, the display list is invalidated through the setter methods, and the component is then redrawn.

<br/><br/>

<div style="overflow-x: visible;">
<code><pre>package
{
  import mx.core.UIComponent;

  public class CircleRenderer extends UIComponent
  {
    private var _color : Number = 0;
    private var _size : Number = 0;
    
    public function CircleRenderer()
    {
      super();
    }
    
    public function set color( value : Number ) : void
    {
      _color = value;
      invalidateDisplayList();
    }
    
    public function get color() : Number
    {
      return _color;
    }
    
    public function set size( value : Number ) : void
    {
      _size = value;
      invalidateDisplayList();
    }
    
    public function get size() : Number
    {
      return _size;
    }
    
    override protected function updateDisplayList(w:Number, h:Number):void
    {
      with (graphics)
      {
        clear();
        beginFill( _color );
        drawCircle( 0,0,_size );
      }
    }
  }
}

</pre></code>
</div>

<br/>

Now, let's look at the application that uses the <code>CircleRenderer</code> class.   Notice that the root application has two event listeners -- <code>creationComplete</code> and <code>mouseDown</code>.   The <code>creationComplete</code> event triggers the <code>onCreationComplete()</code> function, which creates 50 <code>CircleRenderer</code> instances, each with a random color, and calls the <code>randomize()</code> function.   The <code>mouseDown</code> event triggers the <code>onMouseDown()</code> function, which loops through each of the <code>CircleRenderer</code> instances and calls the <code>randomize()</code> function on each.

<br/><br/>

<div style="overflow-x: visible;"><code><pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application 
  xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="absolute"
  creationComplete="onCreationComplete()"
  mouseDown="onMouseDown()"
  horizontalScrollPolicy="off"
  verticalScrollPolicy="off"
  backgroundColor="#FFFFFF" 
  viewSourceURL="srcview/index.html"&gt;
  
  &lt;mx:Script&gt;
    &lt;![CDATA[
      import caurina.transitions.Tweener;
      
      private var circles : Array = [];
      
      private function onCreationComplete() : void
      {
        //create a bunch of CircleRenderer objects
        for ( var z:int = 0; z &lt; 50; z++)
        {
          var cr : CircleRenderer = new CircleRenderer();
          cr.color = Math.random() * 0xFFFFFF;
          circles.push( cr );
          addChild( cr );
          randomize( cr );
        }
      }
      
      private function onMouseDown() : void
      {
        for each ( var cr : CircleRenderer in circles )
        {
          randomize( cr );
        }
      }
      
      private function randomize( cr : CircleRenderer ) : void
      {
        Tweener.addTween( cr,
          {
            size: Math.random() * 300,
            alpha: Math.random(),
            x: Math.random() * width,
            y: Math.random() * height,
            time: 1,            
            transition: "easeInOutQuart"
          } );
      }
      
    ]]&gt;
  &lt;/mx:Script&gt;
  
&lt;/mx:Application&gt;</pre></code>
</div>

<br/>

Finally, let's examine the <code>randomize()</code> function -- This is where Tweener comes into play.   The randomize function makes only one function call.   It calls the <code>Tweener.addTween</code> function.  <code>Tweener.addTween</code> accepts two parameters.   The first is the object which will have it's properties modified.  In this case, it refers to the <code>CircleRenderer</code> instance passed into the <code>randomize()</code> function.   The second paramer is a generic object that specifies the target properties and their target values.  This specifies which values will be updated on the object passed into the first parameter.   The code below will instruct Tweener to animate the <code>size</code>, <code>alpha</code>, <code>x</code>, and <code>y</code> properties on the <code>CircleRenderer</code> instance from their current value to the value specified in this object.   You will also notice two additional properties -- <code>time</code> and <code>transition</code>.   The <code>time</code> property specifies the duration of the animation in seconds.  The value <code>1</code> specifies an animation duration of one second (this is NOT milliseconds). The <code>transition</code> property is a string value that specifies the easing transition that gets used in the animation.   You can see all of the "out-of-the-box" easing transitions in the <a href="http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html" target="_blank">Tweener online documentation</a>.

<br/><br/>

<strong>Related Links<br/></strong><br/>

Tweener on Google Code:<br/>
<a target="_blank" href="http://code.google.com/p/tweener/">http://code.google.com/p/tweener/</a><br/><br/>

Tweener Online Documentation:<br/>
<a target="_blank" href="http://hosted.zeh.com.br/tweener/docs/en-us/">http://hosted.zeh.com.br/tweener/docs/en-us/</a><br/><br/>

Tweener Transitions:<br/>
<a target="_blank" href="http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html">http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html</a><br/><br/>

View this example in a new window:<br/>
<a target="_blank" href="http://www.tricedesigns.com/portfolio/tweener/">http://www.tricedesigns.com/portfolio/tweener/</a><br/><br/>

View/Download example source:<br/>
<a target="_blank" href="http://www.tricedesigns.com/portfolio/tweener/srcview/index.html">http://www.tricedesigns.com/portfolio/tweener/srcview/index.html</a>


<br/><br/>
___________________________________<br/>
<strong>Andrew Trice</strong><br/>
Principal Architect<br/>
<a href="http://www.cynergysystems.com" target="_blank">Cynergy Systems<br/>
http://www.cynergysystems.com</a>
<br/><br/>



]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055005</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055005" />
    <title>Comment from Ramón Helena on 2009-03-12</title>
    <author>
        <name>Ramón Helena</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi Andrew!!</p>

<p>Very cool example,</p>

<p>Thanks for shared it,</p>

<p>Einfach Klasse!</p>

<p>Best,</p>

<p>Ramón</p>]]>
    </content>
    <published>2009-03-12T08:53:08Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055165</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055165" />
    <title>Comment from greg on 2009-03-14</title>
    <author>
        <name>greg</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi!</p>

<p>Did you ever tried GTween library from Grant Skinner?<br />
What do you think about that?</p>

<p>Thanks.</p>]]>
    </content>
    <published>2009-03-14T11:35:24Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055174</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055174" />
    <title>Comment from Andrew Trice on 2009-03-14</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>I've heard of it, but never used it.   The demo's i've seen look very good, but I can't comment b/c I have never tried it.</p>]]>
    </content>
    <published>2009-03-14T15:54:45Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055208</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055208" />
    <title>Comment from Philipp on 2009-03-15</title>
    <author>
        <name>Philipp</name>
        <uri>http://blog.gsdh.de/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blog.gsdh.de/">
        <![CDATA[<p>nice one thanks... :)</p>]]>
    </content>
    <published>2009-03-15T12:14:59Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055212</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055212" />
    <title>Comment from Nicolas on 2009-03-15</title>
    <author>
        <name>Nicolas</name>
        <uri>http://stoletheshow.com/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://stoletheshow.com/">
        <![CDATA[<p>As a Flash developer, I have been using Caurina Tweener for many projects in the past. I have just recently switched to TweenMax from GreenSock and can highly recommend it.</p>]]>
    </content>
    <published>2009-03-15T16:30:27Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35576-comment:2055233</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35576" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/03/using-tweener-for-scripted-ani.html#comment-2055233" />
    <title>Comment from Vincent on 2009-03-15</title>
    <author>
        <name>Vincent</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>great.Thanks for share it.</p>]]>
    </content>
    <published>2009-03-16T03:03:09Z</published>
  </entry>

</feed
