2007年5月14日星期一

Render Event

ActionScript has long since relied on the enterFrame (onEnterFrame) event for time-based actions, especially animation and actions relating to frame playback. In comparison, Director's Lingo language has, not only an enterFrame event, but two other frame events, prepareFrame and exitFrame. Though ActionScript 3 has not aquired prepareFrame or exitFrame, it has gained a new frame event, render, or Event.RENDER (flash.events.Event.RENDER).

The render event in AS3 is a frame event that occurs after enterFrame (flash.events.Event.ENTER_FRAME) allowing one more chance to do what you need to do before the screen updates its display.

Unlike enterFrame, however, the render event will not be called unless the display object using it is attached to a stage (or in any display list attached to the stage). Also, render is not automatically called every frame, even if attached to the stage. In order for render to be called for the current frame, you must make a call to stage.invalidate() (flash.display.Stage.invalidate());

Ex:

ActionScript Code:

var sprite:Sprite = new Sprite();
stage.addChild(sprite);

sprite.
addEventListener(Event.ENTER_FRAME, enterFrame);
sprite.
addEventListener(Event.RENDER, render);
stage.addEventListener(MouseEvent.CLICK, click);

function enterFrame(event:Event):
void {
trace("enter frame");
}
function render(event:Event):
void {
trace("render");
}
function click(event:MouseEvent):
void {
trace("click");
stage.invalidate();
}

Code:

Output:

enter frame

enter frame

enter frame

click

enter frame

render

enter frame

enter frame

enter frame

enter frame

...

0 评论: