Flash main loop

Flash is platform like another one, but specific is that we have a quarantined loop cycle.

That means if we set 30 FPS in our app/game that in the best way we achieve exactly 30FPS (or less in some cases), and each next ENTER_FRAME broadcast event will be dispatched in next 33,(3)ms. Rest of this time will be spend in idle mode.

Let’s have a look closer on the each element of main loop.

1.ENTER_FRAME

This is most interesting phase and most heavy for CPU.

This event do not ensure that we have access to children by getChildByName or getChildAt. Sound weird but it is. This happens only in the specific scenario. First of all this happens when we compose scene by Flash Professional IDE and we will attach ENTER_FRAME handler to MovieClip with more than one frame and we will try to get under-construct child (yet).

In this phase each under-construct children placed on stage will be constructed . Mostly by standard constructor of MovieClip/Sprite or linked class in Flash Professional.

If we link following class to the object and we will place it onto the stage we will see output:– Hello– MC: onAdded

1

2

3

4

5

6

7

8

9

10

11

12

13

packageeu.trzeci.test{

importflash.display.MovieClip;

publicclassMC{

publicfunctionMC(){

trace("Hello");stage.addEventListener(Event.ADDED_TO_STAGE,onAdded);

}

publicfunctiononAdded(event:Event):void{

stage.removeEventListener(Event.ADDED_TO_STAGE,onAdded);

trace("MC: onAdded");

}

}

}

This provides some facts like:

Constructor is launched at first visit main frame

Constructor has access to stage

ADDED_TO_STAGE launches right after class constructor

This is valid for object created by stage (composed in Flash Professional)

If in some place of our code we can use:

1

newsome_animation();

Then we receive Exception about null reference to stage. That’s why we should protect access to stage and ensure that stage != null.

2. FRAME_CONSTRUCTED

This phase ensures we have access to main graph of stage including children and parents. getChildByName() and getChildAt() work as we expect.