0

I have a holder movieclip, its base class is foo.

package {
    import flash.display.MovieClip;

    public class Foo extends MovieClip {
        public function Foo() {
            trace("foo");
        }
    }
}

Within foo are a number of other movieclips, with a base class of bar.

package {
    import flash.display.MovieClip;

    public class Bar extends MovieClip {
        public function Bar() {
            trace("bar");
        }
    }
}

I put a trace in the constructor of bar so I can tell if it's being loaded properly, and when I drag out foo onto the scene and run the clip, all the little bars within it fire off correctly. However, when I add it to the scene dynamically, such as like this in the Main class:

package {
    import flash.display.MovieClip;

    public class Main extends MovieClip {
        public function Main() {
            this.addChild(new Foo());
        }
    }
}

Suddenly, all the little bar movieclips within foo revert to regular old movieclips and don't fire. Is there any way to fix this?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Lance
  • 1
  • 1
  • You should post a zip of your fla and as files. I just replicated your file based on your description on the Bar constructors fired properly. Meaning something else might be at work, maybe your doc class isn't linked? – Zevan Dec 19 '10 at 01:41
  • Sure, added them to the end of the original post – Lance Dec 19 '10 at 06:06

1 Answers1

0

getChildAt() returns a DisplayObject. In order to access any of the Bar methods/properties, you need to explicitly cast it as a Bar object.

Using your code for the example...

public class Main extends MovieClip {
    public function Main() {
        var foo:Foo = new Foo();
        stage.addChild(foo);
        foo.x = 0;
        foo.y = 0;

        trace(foo.getChildAt(1) as Bar);
    }//Main()
}

Oddly enough, (very oddly) I've just noticed that Flash doesn't run Bar's custom constructor unless there is code SOMEWHERE declaring those objects within Foo as being of the Bar type. By simply modifying the one trace statement above (so that it casts the return of getChildAt) all Bar objects present within Foo ran their custom constructors. Go figure.

greatdecay
  • 246
  • 1
  • 3