Updated: June 15th, 2009

Archive for June, 2009

The MovieClip life cycle revisited. From Event.ADDED to Event.REMOVED_FROM_STAGE.

iconJune 15th, 2009 icon7 Comments

This post is a follow up to my previous post on the MovieClip life cycle. Previously I took a look at the sequence of events that occur during the life of a single frame. This post adds to the discussion by covering event types relevant to MovieClip display object children. Those events are Event.ADDED, Event.ADDED_TO_STAGE, Event.REMOVED, and Event.REMOVED_FROM_STAGE.

What follows is the same order of events from before, plus the four new events. The four have been around since Flash Player 9, they’re just new to my discussion. And to keep things short, I’m only going to discuss the new events in depth.

  1. Event of event type Event.ENTER_FRAME dispatched
  2. Constructor code of children display objects is executed
  3. Event of event type Event.ADDED dispatched from children display objects
  4. Event of event type Event.ADDED_TO_STAGE dispatched from children display objects
  5. Event of event type Event.FRAME_CONSTRUCTED dispatched
  6. MovieClip frame actions are executed
  7. Frame actions of children MovieClips are executed
  8. Event of event type Event.EXIT_FRAME dispatched
  9. Event of event type Event.RENDER dispatched
  10. Event of event type Event.REMOVED dispatched from children display objects
  11. Event of event type Event.REMOVED_FROM_STAGE dispatched from children display objects

A special note about these events. The order of the above is only specific to MovieClips added or removed from the timeline in the Flash IDE. Manually calling addChild() or addChildAt() will dispatch Event.ADDED and Event.ADDED_TO_STAGE events immediately after the add child call. The same holds true for the removeChild() and removeChildAt() calls, and the related Event.REMOVED and Event.REMOVED_FROM_STAGE events.

Steps 3 and 4

Event.ADDED and Event.ADDED_TO_STAGE are both dispatched when the playhead moves across the MovieClip timeline and encounters a keyframe with a new child display object. If the child exists in the frame immediately previous to the keyframe there a number of conditions that will still cause both events to be dispatched. Both events will be dispatched if the child exists on a different layer, or has a different instance name, or is being masked/unmasked. Masked is defined as the mask being added by keyframe to the mask layer. Unmasked means that in the next frame the mask layer is empty.

Regardless of the order that listeners are added, Event.ADDED is dispatched before Event.ADDED_TO_STAGE. If a child, in the frame the playhead is on, is not being added neither event will be dispatched.

Steps 10 and 11

Event.REMOVED and Event.REMOVED_FROM_STAGE are dispatched at the end of the current frame when a child exists on the timeline and does not exist in the next frame. There are other conditions that cause the remove events to be dispatched from a child that remains on the timeline. Conditions mirror those of the added events and include when the child in question appears on a new keyframe on a different layer, or the child has a different instance name, or the child is about to be masked/unmasked.

Regardless of the order that listeners are added, Event.REMOVED is dispatched before Event.REMOVED_FROM_STAGE. If a child is not being removed in the next frame, then neither event will be dispatched.

The Author

Johannes Tacskovics
ECMAScript Developer

Portrait of Johannes Tacskovics

On front-end software engineering this is the blog of Johannes Tacskovics, a Los Angeles based ECMAScript Developer. This blog is focused on object oriented software development for the web.

Article Categories

Article Archives