Do you remember the effect seen in the article:
Inertia with Flash CS3 '
Many scripts created by FlepStudio, are surely didactically valid, but if they are not applied to each of us imagination, they are not worth much.
Reason for which, with this article I would want to show you what can be done with the inertia effect applied to more MovieClips.
Follow me'
I create a FLA and save it as 'inerzia_multipla.fla', into which I create a MovieClip named 'mc_clip'.
I right click the 'mc_clip' in library and I assign a linkage name, the Clip class that I will create next.
I create a Document Class, an AS file saved as 'Multi.as', implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Multi extends MovieClip
{
private var clips_array:Array;
private var numClips:int=50;
public function Multi()
{
init();
}
private function init():void
{
stage.frameRate=31;
clips_array=new Array();
attachClips();
go();
}
private function attachClips():void
{
for(var i:int=0;i < numClips;i++)
{
var clip_mc:Clip=new Clip();
addChild(clip_mc);
clips_array.push(clip_mc);
clip_mc.x=stage.stageWidth/2;
clip_mc.y=stage.stageHeight/2;
}
clips_array.reverse();
}
private function go():void
{
clips_array[0].buttonMode=true;
clips_array[0].addEventListener(MouseEvent.MOUSE_DOWN,moveMe);
clips_array[0].addEventListener(MouseEvent.MOUSE_UP,dropMe);
addEventListener(Event.ENTER_FRAME,gogo);
}
private function moveMe(m:MouseEvent):void
{
m.target.startDrag();
}
private function dropMe(m:MouseEvent):void
{
m.target.stopDrag();
}
private function gogo(e:Event):void
{
for(var i:int=1;i < clips_array.length;i++)
{
clips_array[i].x+=(clips_array[i-1].x-clips_array[i].x)*.3;
clips_array[i].y+=(clips_array[i-1].y-clips_array[i].y)*.3;
}
}
}
}
Now i create Clip.as:
Code:
package
{
import flash.display.MovieClip;
public class Clip extends MovieClip
{
public function Clip()
{
}
}
}
Here is the result (drag the orange ball to see the effect):
Let's analyse the code:
Properties
An array into which I will insert each clip created (Clip class)
private var clips_array:Array;
A variable containing the number of MovieClip needed
private var numClips:int=50;
Methods
init();
I impost the frame rate
stage.frameRate=31;
I initialize the array
clips_array=new Array();
I call the attachClips() method
attachClips();
I call the method go()
go();
attachClips();
I create a cycle using the value of the variable 'numClips' as a maximum iteration
for(var i:int=0;i < numClips;i++)
{
I create a new instance of the Clip class (nothing else then the MovieClip 'mc_clip' in library)
var clip_mc:Clip=new Clip();
I add it to the DisplayObject
addChild(clip_mc);
I insert it to the array to keep track of it and be able to call it when needed
clips_array.push(clip_mc);
I position the clip
clip_mc.x=stage.stageWidth/2;
clip_mc.y=stage.stageHeight/2;
}
I reverse the array. The first clip created will pass last so that the last clip will become first clips_array.reverse();
go();
I active the button mode when the mouse pass over the first clip of the array clips_array[0].buttonMode=true;
I add two listeners to the clip's event MOUSE_DOWN and MOUSE_UP which will call the methods movete() and dropMe() clips_array[0].addEventListener(MouseEvent.MOUSE_DOWN,moveMe);
clips_array[0].addEventListener(MouseEvent.MOUSE_UP,dropMe);
I add an listener to the interval ENTER_FRAME which will call the function gogo() as many times by seconds based on the frame rate addEventListener(Event.ENTER_FRAME,gogo);
moveMe();
I start the dragging of the clip
m.target.startDrag();
moveMe();
I stop the dragging of the clip
m.target.stopDrag();
gogo();
I create a cycle with 1 as a minimum iteration (so that the first drag able clip is not included) and clips_array length as a maximum iteration
for(var i:int=1;i < clips_array.length;i++)
{
I apply the inertia effect to all the clips except the first one which is drag able.
clips_array[i].x+=(clips_array[i-1].x-clips_array[i].x)*.3;
same thing for the clip's Y
clips_array[i].y+=(clips_array[i-1].y-clips_array[i].y)*.3;
}
Source files: