Back to "flash Basic Theory Class-Catalog"
Animation events
We want to be able to use code to make things move and allow the screen to refresh repeatedly. I've seen an example of using the Enterframe movie event earlier. Now apply this method to as 3, just add a listener to the Enterframe event:
addEventListener(Event.ENTER_FRAME, onEnterFrame);
Don't forget to import the Event class and create a method named Onenterframe. People often confuse, only one frame how can execute Enterframe (enter frame) event? In fact, the playhead is not really in the next frame, it only stays on the first frame, not to move the playhead to the next frame to form the Enterframe event, but in another way: Flash tells the player when to move, you can see Enterframe as a timer, Just a little imprecise.
Now let's take a look at the first as 3 animation:
package {
import flash.display.Sprite;
import flash.events.Event;
public class FirstAnimation extends Sprite {
private var ball:Sprite;
public function FirstAnimation() {
init();
}
private function init():void {
ball = new Sprite();
addChild(ball);
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(0, 0, 40);
ball.graphics.endFill();
ball.x = 20;
ball.y = stage.stageHeight / 2;
ball.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
ball.x++;
}
}
}
The init function creates an sprite movie named Ball and establishes an event listener for it. The Onenterframe function is responsible for ball movement and screen refresh work. This is the basis for learning the content of this book, but also the basis for creating animations using ActionScript, so be sure to master it.