3 Doors Down MP3 songs Music download Edge Of Sanity Gipsy Kings Coil Download MP3 Verve She and Him Billy Talent Thraw Elton John MP3 site

Animation Using TweenLite

In this tutorial we're going to use TweenLite to move a cube around. This will teach you basics of animation, and how to use TweenLite.

download-source

animating-using-tweenlite

In this example our solid red cube is trying to stay inside our green wireframe cube. Every time the red cube gets itself positioned completely inside the green cube, the green cube moves.

TweenLite is the package which is smoothly moving the red cube around. The TweenLite package is also firing the event to move the green cube when the tween is complete.

1: To start off, you'll need TweenLite. You can download it here if you don't already have it (also included in the source download "gs" folder): http://blog.greensock.com/tweenlite/

2: Import tweener - Add this line to your imports:

Actionscript:
  1. import gs.TweenLite;

3: Add a Tween. You can add tweens to any parameter of any object. The TweenLite package will gradually change any parameter in your code to a new value over time, so only numeric values will work nicely.

To add a tween, we use the code:

Actionscript:
  1. //TweenLite.to(objectToAnimate, time, { property:newValue, property:newValue, ease:Quint.easeInOut, onComplete:functionToCall} );
  2.  
  3. //Your code will end up looking something like this
  4. TweenLite.to(cube, 2, { x:xp, z:yp, ease:Quint.easeInOut, onComplete:randomize } );

The line above will add a tween. This tween will change the values "x" and "y" on the object "cube" to 1000 and 500 respectively. It will smoothly change the values over a period of 2 seconds and once the tween is complete and the cube.x is 1000 and cube.y is 500, it will trigger the "randomize" function.

Experiment with more ways to tween your objects. You can apply tweens to anything, including cameras, lights, rotations, sizes and camera zooms. Any numerical value you can think of.

Actionscript:
  1. package
  2. {
  3.     import gs.easing.Quint;
  4.     import gs.TweenLite;
  5.     import org.papervision3d.lights.PointLight3D;
  6.     import org.papervision3d.materials.ColorMaterial;
  7.     import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  8.     import org.papervision3d.materials.utils.MaterialsList;
  9.     import org.papervision3d.materials.WireframeMaterial;
  10.     import org.papervision3d.objects.primitives.Cube;
  11.     import org.papervision3d.objects.primitives.Plane;
  12.     import org.papervision3d.view.BasicView;
  13.    
  14.     /**
  15.      * ...
  16.      * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
  17.      */
  18.    
  19.     public class Main extends BasicView
  20.     {
  21.         protected var cube:Cube;
  22.         protected var cube2:Cube;
  23.         protected var mat:FlatShadeMaterial;
  24.         protected var mat2:WireframeMaterial;
  25.         protected var plane:Plane;
  26.        
  27.         public function Main():void
  28.         {
  29.             super();
  30.             init();
  31.         }
  32.         protected function init():void
  33.         {
  34.             createChildren();
  35.             commitProperties();
  36.             randomize();
  37.             startRendering();
  38.         }
  39.         protected function createChildren():void
  40.         {
  41.             //Create Materials:
  42.             mat     = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
  43.             mat2    = new WireframeMaterial(0x00FF00);
  44.            
  45.             //Create 3D Objects
  46.             plane   = new Plane(null, 2000, 2000, 10, 10);
  47.             cube    = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
  48.             cube2   = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
  49.            
  50.             //Add objects to the scene
  51.             scene.addChild(plane);
  52.             scene.addChild(cube);
  53.             scene.addChild(cube2);
  54.         }
  55.         protected function commitProperties():void
  56.         {
  57.             //Set properties of our plane
  58.             plane.material.lineColor = 0x777777;
  59.             plane.material.doubleSided = true;
  60.             plane.pitch(90);
  61.             plane.y     = -50;
  62.            
  63.             //Set the properties of our camera
  64.             camera.x    = 0;
  65.             camera.z    = 1000;
  66.             camera.y    = 1000;
  67.         }
  68.        
  69.         public function randomize():void
  70.         {
  71.             var xp:Number   = (Math.random() * 2000) - 1000;
  72.             var yp:Number   = (Math.random() * 2000) - 1000;
  73.             cube2.x         = xp;
  74.             cube2.z         = yp;
  75.            
  76.             //When complete it calls this method again
  77.             TweenLite.to(cube, 2, { x:xp, z:yp, ease:Quint.easeInOut, onComplete:randomize } );
  78.         }
  79.     }
  80. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

1 Responses to “Animation Using TweenLite”


Leave a Reply


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed