Almer/Blank Labs

Capturing ADDED_TO_STAGE in ActionScript 3

The ADDED_TO_STAGE event is a really handy one in AS3 -- it tells you when a display object has been added to the display list. But I'd been wondering if there were a way to put a single ADDED_TO_STAGE event listener on the stage, to know when anything was added to the display list. My first attempt did not work -- but then I tried listening during the capture phase. And VOILA! It works. Note the 'true' argument (the 3rd one) in addEventListener -- that makes the listener listen during the capture phase. I'm not sure why this event is not hearable in the bubble phase (I'm sure there's a very good, very technical reason), but there you have it. Here's the logic:
  1. stage.addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage , true ) ;
  2. function onAddedToStage ( evt )
  3. {
  4. trace ( "onAddedToStage, evt: " + evt.target ) ;
  5. }
  6. addChild ( new MovieClip ( ) ) ;
Share and enjoy!

Category: Code & Samples

Tagged: , , ,

Leave a Reply