Listening for events can be expensive. For a handler to receive an event, Flash must create an object in memory that represents that event. Repeatedly allocating memory can hurt the performance of your app, and this is especially true if you are listening for Event.ENTER_FRAME
from a large number of display objects.
Let us see how to minimize the impact of handling multiple ENTER_FRAME
events by making some adjustments to the architecture of your example app. We will also add some additional code to individually control the speed of each bubble, making the app a little more polished.
You will be required to make code changes to both Main.as
and Bubble.as
:
First start by opening
Bubble.as
.Within the constructor, remove the highlighted line of code that listens for
Event.ENTER_FRAME
:public function Bubble() { addEventListener(Event.ENTER_FRAME, enterFrame); }
Change the
enterFrame()
method's signature by renaming it toupdate()
and removing itsEvent
parameter...