You'll have often come across animations or effects simulating Physics or Maths laws with Actionscript.
What I want to introduce is a long chapter not always easy to understand.
Watch this space to see some really interesting things!
So, let us begin...
The first effect useful to learn is called ' inertial movement ' .
In a few words it"s about those animations that start fast and increasingly slow down to a stand still point.
Obviously I"m referring to object moving animations with Actionscript, but this effect is also applicable to other properties like the alpha or the rotation, just to cite a couple of examples.
Let"s see the juice:
I create an FLA and save it as " inerzia.fla " then I create the Document Class, an AS file that I save as " Inerzia ".
Here it is:
Code:
package
{
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
public class Inerzia extends Sprite
{
private var quadrato:Sprite;
private var arrivo:int=400;
public function Inerzia()
{
stage.frameRate=31;
creaSprite();
initQuadratoListener();
initBottoneListener();
}
private function creaSprite():void
{
quadrato=new Sprite();
quadrato.graphics.beginFill(0xFF0066);
quadrato.graphics.drawRect(100,100,50,50);
quadrato.graphics.endFill();
addChild(quadrato);
}
private function initQuadratoListener():void
{
quadrato.addEventListener(Event.ENTER_FRAME,muoviQuadrato);
}
private function initBottoneListener():void
{
riprova_btn.addEventListener(MouseEvent.MOUSE_DOWN,replay);
}
private function muoviQuadrato(e:Event):void
{
var distanza:Number=arrivo-quadrato.x;
var inerziale:Number=distanza*.1;
quadrato.x+=inerziale;
if(Math.abs(distanza)<=.5)
{
quadrato.x=arrivo;
quadrato.removeEventListener(Event.ENTER_FRAME,muoviQuadrato);
}
}
private function replay(m:MouseEvent):void
{
quadrato.removeEventListener(Event.ENTER_FRAME,muoviQuadrato);
quadrato.x=0;
initQuadratoListener();
}
}
}
Here is the result:
Note that the class extends the Sprite class and not the MovieClip simply because I reduce the memory and I don"t import the MovieClip class, but only the Sprite one.
If the MovieClip (s) are hand-drawn in the FLA, then the class will have to extend the MovieClip class.
What shall I do:
I create a variable called " arrivo "
I draw a simple square Sprite and I assign a certain X and Y to it.
I add the listeners ( in this case there"s also a listener for the " Riprova " button)
I start an interval ( ENTER_FRAME )
The variable " distanza " is always the difference between the end point ( the value of the variable " arrivo ") and the X of the Sprite.
The variable " inerziale " divides the " distanza " by 10, as * .1 equals to /10
Let"s remember always that we have to interrupt the interval when the Sprite reaches the end point or we"ll always have an open interval, with CPU consequences for the user.
Therefore I stop the interval when the value of the variable " distanza " is less than or equal to 0.5 .
Stay tuned!