electronslibres.ca ça mute avec l'AS3

7Jan/102

Slow Mo’

I recently stumbled on this amazing japanese flash coder Yasunobu Ikeda and one of his experiment on slow motion. Digging into his code, I realized he was using BetweenAS3 tweening engine. I was already very comfortable with Jack Doyle's TweenLite, but I had not tried the TimelineLite feature. You can download the source code for JD's TimelineLite engine here.

This engine has the ability to create tweens sequences as in a Timeline. Like a multitrack recording machine, you can choose to add a tween at the end of the sequence (serial tweenings) or in a parrallel manner then control all these tweens individually or at once for conveniance you will soon understand.

This movie shows cubes that bounce on a plane ground. 2.4 seconds after the sequence of tweens is initiated, the time mapping property of the Timeline is changed to achieve the slow motion effect.

first, let's create those cubes and a virtual ground for them to simulate a bounce:

var floor: Plane = scene.addChild(new Plane(new WireframeMaterial(0x666666), 5000, 5000, 15, 15)) as Plane;
floor.rotationX = 90;
 
// create TimeLine instance to store the cube tweens
myTimeline = new TimelineLite({onUpdate:checkTime});
 
// add the camera tween to the sequence
myTimeline.insert( new TweenLite(camera, 6, {y:400, zoom:4, ease:Cubic.easeInOut}) );

Then we want each cube to fall down with a bounce effect as if they were bouncing on the ground. So let's add a tween with an easing set to bounce:

// number of cubes on the scene
static private const OBJ_NUM :int = 20;
 
// define a composite material for all the cubes
var mt:CompositeMaterial = new CompositeMaterial();
mt.addMaterial( new ColorMaterial(0x0, 0.6) );
 
// creating 3D Objects and tweening each object
for (var i:int = 0; i < OBJ_NUM; i++)
{
// create a cube with the same composite material for all its faces
var cube:Cube = scene.addChild(new Cube(new MaterialsList( { all:mt } ), 100, 100, 100)) as Cube;
 
// random position for each cube on the scene
cube.x = 1500 * Math.random() - 750;
cube.y = 2000;
cube.z = 1500 * Math.random() - 750;
 
// random duration for each tween (from 3 to 5 seconds)
var sec:Number = 2 * Math.random() + 3;
 
// add tween to myTimeline sequence
myTimeline.insert( new TweenLite(cube, sec, {y:50, ease:Bounce.easeOut}) );
}

One of the great features TimelineLite offers is the ability to re-interpret the Time of the sequence with a property called timeScale. By default, timeScale = 1 which means time is unchanged. If you increase its value, the sequence will play faster, and vice versa. If timeScale = 2, your sequence will play two times faster, and if timeScale = 0.5, the sequence of tweens will play at half speed!

TimelineLite also features a currentTime property, and a totalDuration property. I chose to calculate a percentage for the sequence advancement. As I used a random duration for each tween, I don't know which one is the longest, and I don't need to! Whenever the movie has executed 40% of the duration of the sequence, it shifts timeScale 10 times slower than normal speed for 5 % of the sequence time, and then back to normal speed. And there you have it ! Real slow Motion HD effect !

private function checkTime():void
{
var percent:int = Math.round(myTimeline.currentTime / myTimeline.totalDuration  * 100);
switch (percent)
{
case 40:
myTimeline.timeScale = 0.1;
break;
case 45:
myTimeline.timeScale = 1;
break;
}

source code is available here
Papervision classes are available here, and gs TimelineLite here.

Cheers!

Comments (2) Trackbacks (0)
  1. Very cool example, Fred. Thanks so much for sharing. One minor correction – Grant Skinner isn’t the author of TweenLite. http://blog.greensock.com/about/ :)

    Anyway, I really like how you demonstrated the value of the timeScale property. You could even tween it if you want a smooth transition between normal and slow motion. And tweens aren’t limited to being organized in a serial or parallel manner – they can overlap as much as you want. There’s a video that explains it at http://blog.greensock.com/timeline-basics/

    Also, instead of using an onUpdate to check when it hits 40% and 45%, you could just use TweenLite.delayedCall() or switch to TimelineMax and use addCallback() – either way, it would call a function at a specific time that’d change (or tween) the timeScale. Just a few ideas. But you did a great job here – I totally appreciate the demo. Cheers!

  2. One minor correction – Grant Skinner isn’t the author of TweenLite. http://blog.greensock.com/about/ :)

    I am really sorry about the authoring mistake. I suppose I have seen those import gs.* and import gs.easing.* statements too many times and my mind in a perverted twist assimilated those with Grant’s acronyme. At least it is now corrected!

    You could even tween it if you want a smooth transition between normal and slow motion. And tweens aren’t limited to being organized in a serial or parallel manner – they can overlap as much as you want. There’s a video that explains it at http://blog.greensock.com/timeline-basics/

    Indeed, and the video is a great introduction to all the features this amazing Timeline engine brings to life.

    Also, instead of using an onUpdate to check when it hits 40% and 45%, you could just use TweenLite.delayedCall()

    Yup, and I will add an update to this example with a simpler version and delayedCall in a near future!

    Again, thx for your review and this amazing library!


Leave a comment


No trackbacks yet.