Optimization with the Event.RENDER Event

The Event.RENDER event is a specialized type of screen-update event used in advanced situations where graphics performance is critical. Its primary purpose is to let the programmer defer the execution of all custom drawing routines until the precise moment before the screen is rendered, thus eliminating duplicate drawing-routine execution. Unlike all other built-in Flash runtime events, the Event.RENDER event must be requested manually by the programmer. The Flash runtime dispatches the Event.RENDER event when the following two conditions are both true:

  • The Flash runtime is about to check if the screen needs updating (whether due to a frame passing or an updateAfterEvent( ) call).

  • The programmer has invoked stage.invalidate( ). (stage.invalidate( ) is the programmer's way of asking the Flash runtime to dispatch the Event.RENDER event the next time a screen-update check occurs).

Let's consider an example showing how the Event.RENDER event can be used to improve performance. Suppose we're creating an Ellipse class that represents an on-screen ellipse shape. For the sake of simplicity, assume that the ellipse is always filled white, with a black, 1 pixel-thick outline. Our Ellipse class has two responsibilities:

  • It must manage the conceptual data for an ellipse (i.e., store the ellipse width and height).

  • It must draw an onscreen ellipse based on the conceptual ellipse and redraw the onscreen ellipse if the conceptual ellipse changes.

Given the preceding ...

Get Essential ActionScript 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.