1

Alright, so I'm trying to figure out when a child is added to a movieclip "x", and handling/detouring this operation from within this "x" movieclip.

I tried overriding addChild and addChildAt at with no prevail. The movieclips that are placed on the stage via flash still don't trigger addChild or addChildAt. However, tracing this.numChildren shows '2' correctly.

Any hints?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145

2 Answers2

2

You can add an event listener for the "added" event for the x movie clip.

x.addEventListener(Event.ADDED, addHandler);

function addHandler(e:Event){
 // your code here
}

This link may explain it better: AS3.0 – Event.ADDED and Event.ADDED_TO_STAGE

The documentation is also a good resource: flash.events.Event

stormwild
  • 2,855
  • 2
  • 31
  • 38
  • 1
    Although correct, it won't detect when x has something added to it, rather it will detect when it itself is added to something. Unless of course I misread the original question – Chris Apr 12 '11 at 08:43
  • Nope, this is when you want to track when the container is added, not when children are added to it. – Qix - MONICA WAS MISTREATED Apr 12 '11 at 21:31
1

You can override the default methods of a movieclip by doing the following:

Create a class to extend a movieclip:

package  {
    import flash.display.*;

    public class SuperMovieClip extends MovieClip {

        public function SuperMovieClip() {
            // constructor code
            super();
        }

        override public function addChild(child:DisplayObject):DisplayObject {
            trace("Hello, I am overriding add child");
                    // still perform the default behavior but you can do what ever you want. 
            return super.addChild(child);                       
        }

    }

}

Then in Flash create a new movieclip, and make sure it is marked as Enable for ActionScript. The Class should be any name you want, but the base class needs to be SuperMovieClip (or the name you chose for your extended class) See image:

enter image description here

Now when any stage clip is created of this base type (regardless if it's in the IDE or through code) it will be of type SuperMovieClip and anytime addChild is called it will override the original function.

For example, I placed an instance of this mc from library onto the stage at design time and compiled it using the following code on the timeline:

import flash.display.Sprite;

stage_mc.addChild(new Sprite());

And it output Hello, I am overriding add child

Chris
  • 54,599
  • 30
  • 149
  • 186
  • Alright, here's what I think I left out in the question: The class that I'm wanting to monitor is the flash's document class. So when the FLA file is compiled from the flash IDE, all of the clips on the timeline are added. These are the ones I want to find. Overriding the document class' addChild method didn't work at all; it never gets called. – Qix - MONICA WAS MISTREATED Apr 12 '11 at 21:30
  • Alright, this would be the ideal answer but I realized the MovieClips on the timeline are added to the stage first (which you cannot override), and THEN to the document class instance (which is really weird). I just based all of the instances off a single class that extends MovieClip and then registered them with the document class. It's a pretty roundabout way of doing things but it worked. – Qix - MONICA WAS MISTREATED Apr 13 '11 at 03:45