Animate with Enter_Frame event in AS3
Hi folks, in this post i’m going to show how to animate an object by using Enter_Frame in as3.
Beside commonly used tween class, or third party tweener Enter_Frame event constantly triggering according to the frame of the fla file per second.
In this post i created a Main document class and a circle class as the example, every time you click the circle the circle will move 200 in x-axis;
circle class:
package
{
import flash.display.Sprite;
import flash.events.*;
public class Circle extends Sprite
{
public function Circle()
{
this.addEventListener(MouseEvent.CLICK, movecircle);
}
function movecircle(e:MouseEvent)
{
this.addEventListener(Event.ENTER_FRAME, circleframe);
}
function circleframe(e:Event)
{
var i:int
this.x=this.x+10;
for (i=0; i<10; i++)
{
if(this.x == 200*i)
{
this.removeEventListener(Event.ENTER_FRAME, circleframe);
}
}
}
}
}
2. Main Document Class
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
var circle:Circle = new Circle();
public function Main()
{
circle.x= 150;
addChild(circle);
}
}
}
As you can see what i’m done here, i just created an sweet little handy event handler called circleframe like any other events and execute the method
this.addEventListener(Event.ENTER_FRAME, circleframe);
along with its function:
function circleframe(e:Event)
{
var i:int
this.x=this.x+10;
for (i=0; i<10; i++)
{
if(this.x == 200*i)
{
this.removeEventListener(Event.ENTER_FRAME, circleframe);
}
If you want to stop it just remove it by using any condition.
As you test the movie the first click the circle only movedĀ 50 in x-axis this is because we initiated the circle at 150 x position at the main document class.