12. Animation using Tweener


Hi,

In this tutorial we're going to use Tweener to move a cube around. This will teach you basics of animation, and how to use Tweener (which is awesome).

The example that I've produced is this:

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

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

1: To start off, you'll need Tweener. You can download it here if you don't already have it: http://code.google.com/p/tweener/

Once you've got tweener, either add the source directory containing the "caurina" folder to your project's classpaths, or add the caurina folder into your current project.

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

import caurina.transitions.Tweener;

3:  Add a Tween. You can add tweens to any parameter of any object. The Tweener package will gradually change any parameter in your code to a new value over time, so only numeric values will work nicely. Anyway, you can read a load more about tweener at the google code page above.

To add a tween, we use the code

Tweener.addTween(object, {parameter1: value, parameter2: value2, time: X, transition: "transition", onComplete: functionname});

This may look a little bit confusing at first, but it's really simple. Let's show you a real example..

Tweener.addTween(cube, { x: 1000, z: 500, time: 2, 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.

Here is my source code (Uses latest version of my base class):

Actionscript:
  1. package  {
  2.     import org.papervision3d.lights.PointLight3D;
  3.     import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  4.     import org.papervision3d.materials.utils.MaterialsList;
  5.     import org.papervision3d.materials.WireframeMaterial;
  6.     import org.papervision3d.objects.primitives.Cube;
  7.     import org.papervision3d.objects.primitives.Plane;
  8.     
  9.     import caurina.transitions.Tweener;
  10.     
  11.     public class Main extends PaperBase{
  12.       
  13.        private var cube:Cube;
  14.        private var cube2:Cube;
  15.        private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
  16.        private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
  17.        private var plane:Plane = new Plane(null, 2000, 2000, 10, 10);
  18.       
  19.        public function Main() {
  20.          init(600, 300);
  21.        }
  22.       
  23.        override protected function init3d():void{
  24.          cube = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
  25.          cube.y = 0;
  26.          cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
  27.          cube2.y = 0;
  28.          cube2.x = 1000;
  29.          cube2.z = 1000;
  30.          plane.material.lineColor = 0x777777;
  31.          plane.material.doubleSided = true;
  32.          plane.pitch(90);
  33.          plane.y = -50;
  34.          default_scene.addChild(plane);
  35.          default_scene.addChild(cube);
  36.          default_scene.addChild(cube2);
  37.          Tweener.addTween(cube, { x:1000, z:1000, time:2, onComplete: randomize } );
  38.          default_camera.x = 0;
  39.          default_camera.z = 1000;
  40.          default_camera.y = 1000;
  41.        }
  42.       
  43.        public function randomize():void {
  44.          var xp:Number = (Math.random() * 2000) - 1000;
  45.          var yp:Number = (Math.random() * 2000) - 1000;
  46.          Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
  47.          cube2.x = xp;
  48.          cube2.z = yp;
  49.        }
  50.       
  51.        override protected function processFrame():void{
  52.          default_camera.lookAt(cube);
  53.        }    
  54.     }  
  55. }

22 Comments

  1. Comment by 3rdfloor on March 10, 2008 7:07 pm

    hi there
    did every tutorial on ur site to learn how papervision works. very interesting - thanks for your good work.
    today I played a bit with tweening functions - its really easy to combinate. you can change almost everything during a tween.
    the nicest parameters to change are materialparameters.
    f.ex:
    Tweener.addTween("whatever".material, { fillAlpha: 0.5, time:1 } );
    looks that smooth :-)
    greets

  2. Comment by Mark on March 10, 2008 11:22 pm

    hi there,

    can you post the source for all necessary files for each tutorial?

    my camera perspective isn't changing (like yours is). i think it's related to a changed i missed in my PaperBase class?

    cheers,
    mark

  3. Comment by ohm on March 11, 2008 8:04 pm

    Thx for your tutorials !

    But unfortunately this one must have something missing or some...?
    From your code the Plane doesn't move, and the cubes are not on the plane.

    Take care.

  4. Comment by Luke on March 11, 2008 8:15 pm

    Hi,

    Thanks for the comments!

    @Mark, You can download the most recent base class from the sidebar.. The problem is, you need to change all the references to "Camera3D" to "FreeCamera3D"..

    @ohm, The plane isn't supposed to move :).. Maybe you're having the same camera problem as Mark, if not then I'm not sure what the problem is..

    -Luke

  5. Comment by santhakumar on March 12, 2008 2:34 am

    Hi,
    Really Nice Tutorial.
    Great

  6. Comment by elf on March 19, 2008 11:26 pm

    Very nice tutorial, keep on the good work.

  7. Comment by billybob on March 27, 2008 12:22 am

    Thanks a lot for sharing your knowledge. These tuts are the most didactic i have found so far
    and they really deserve the name 'tutorial'.(A lot of so called tutorials i have found
    about Papervision were just a bunch of code and eventually a reference to an expensive
    workshop) Good job!!

  8. Comment by justin on March 28, 2008 9:05 pm

    I cant see anything when I publish my file. I dont get any errors, but I dont understand wht I am doign wrong...NOOB
    do you need my code to help?

  9. Comment by pixelvoodoo on April 1, 2008 7:22 am

    Hi,

    Quick question. How do I apply a blend mode to an object in the scene, or if it's easier thee whole scene?

    I've stuck this near the top "import flash.display.BlendMode;" and I've tried variations on "cube.blendMode = "MULTIPLY";" everywhere, but no joy.

    Found an example somewhere else doing it, but the code was hardcore and I gave up. Thought you might have a simpler method?

    Thanks, pv

  10. Comment by onlyagame on April 11, 2008 1:35 pm

    i'm getting Viewport3D and BasicRenderEngine errors. The import org.papervision3d.render.BasicRenderEngine; and org.papervision3d.view.Viewport3D; lines in PaperBase.as don't exist in the org directory. am I doing something wrong? actually, more precisely, WHAT am i doing wrong??? ;)

  11. Comment by onlyagame on April 11, 2008 1:45 pm

    doh! downloaded the wrong pv version, now i get ambiguous references to vertex3D. today is not going well :(

  12. Comment by teo on April 26, 2008 2:29 pm

    I have followed the tutorial , everything works fine exept the default_camera.lookAt(cube) that seems not to be interpreted.. the camera doesn't move. I don't know what can be the reason :-(

  13. Comment by alex on April 28, 2008 4:33 am

    hey luke, great tuts - espically the complex collada and tweener animations - though i've got a question that
    you might be able to answer regarding pprvision....

    following your tuts - i've done a little scene in which everything works fine -
    when testing the movie/swf from flash it looks great - one prob

    when uploaded onto the web and into a web page (via the swfobject.js - which you seem to be using aswell)
    none of the collada models appear- -only the superficial 2d _mc's/graphics appear (which would be normally be infront..)

    i've even uploaded all the accompanying files (all the bitmaps and .dae and .fla .as files) to see if that would work
    which it still doesn't :(

    shouldn't the swf be self contained - and run without referencing the actionscript/greatwhite classes all the time...

    cheers - any help would be useful
    i wouldn't of normally posted this here - but i can't seem to access your forums..

    , alex

    (if you wanted to examine them - here are all the files)
    http://www.achmedthesnake.com/idm/

  14. Comment by siniz on June 4, 2008 12:47 pm

    hey Luke,
    big thanx for great tutorial!
    you led me to the first dive into papervision.

  15. Comment by noobie on June 5, 2008 6:17 pm

    Thx Luke for your tuts !
    I'm having a problem tho figuring out how can one add 2d objects over the 3d scene
    overriding your init2d (make a simple hud display with a square, for instance)?
    Anyone can help ?

  16. Comment by John on June 9, 2008 6:22 am

    when will you upload more tutorials ??

    you are great at explaining stuff so simple to understand!

    please keep it up!

  17. Comment by HappyMan on June 21, 2008 11:12 am

    Nice tweener. Is there any twister???

    Luck, why do the .swf that u show here is black background color but mine is white background color?

  18. Comment by Bruno on June 26, 2008 4:39 am

    Downloaded everything from the http://papervision3d.googlecode.com/svn/trunk/branches/GreatWhite/src/ directory today.
    Downloaded the latest version of your PaperBase class.

    But I just can't make it work. I get this error when I try to export movie:
    1020: Method marked override must override another method.

    Can you help with this one? Cheers.

  19. Comment by Bruno on July 1, 2008 9:39 pm

    I found the solution for the erros on the previous post. It was posted by Nik and Ralph Hauwert at http://theflashblog.com/?p=360.

    Just make sure you're not running two versions of Papervision 3D. In Flash IDE go to Edit > Preferences > ActionScript > ActionScript 3.0 Settings... and check if you have any older versions of Papervision 3D in your Classpath.

  20. Comment by patrick on July 2, 2008 7:02 pm

    There is a discrepancy between my version and yours. The camera (zoom?) is different. Could you tell us what you did to change the zoom? I futzed with the camera.zoom prop and that worked but I don't know if there is some other property that's better to use.

    awesome and thank you.

  21. Comment by patrick on July 2, 2008 7:08 pm

    Ah, I didn't have the current versions of PaperBase. Because of this I was using a Camera3D instead of a FreeCamera3D. Interestingly, these two cameras have differing default values for zoom. Camera3D.zoom = 11; FreeCamera3D.zoom = 2;

  22. Comment by Markus on July 4, 2008 10:13 am

    Hey Luke. Great tutorials! Can I put the tweener on a camera? I use the mouse to move the camera and would like to have a nice tweener on the camera.

Comments RSS TrackBack Identifier URI

Leave a comment


Papervision 2 is proudly powered by WordPress and themed by Mukka-mu