views:

26

answers:

1

Can you add a movieclip to the stage from a child class? My setup is as follows:

DocumentClass
->SubFolder
--->ChildClass1
--->ChildClass2

DocumentClass calls ChildClass1 and creates an instance of it. ChildClass1 has in it's constructor a call for ChildClass2 and creates an instance of it.

What is the cleanest way to have ChildClass2, which is the class for a MoveClip, added to the stage? So far, the methods I've tried are:

parent.addChild(ChildClass2)

and

send the stage object to ChildClass1 in it's constructor call on DocumentClass. Then:

stage.addChild(ChildClass2)

Are these the only methods, or are there other ways that don't have required dependencies on other files to add the MovieClip(ChildClass2) to the stage?

Edit: I am working with AS3 and Flash CS5

Edit2: So far the only solution that works for me is passing the stage to ChildClass one and then calling stage.addChild().

+1  A: 

Inside your ChildClass1 instance, that creates your ChildClass2 instance (if I've followed correctly), you can just do:

addChild(childClass2Instance);

This will add it to the stage. I don't see why you'd need to complicate it any further than that, though of course if you want the object to be inside the Document class' coordinate space, then parent.addChild(); should do as you want. I'm not sure if you're saying those two solutions didn't work, or if you're just searching for other possibilities.

In answer to your first question though ("Can you add a movieclip to the stage from a child class?"), yes you can, simply by doing addChild. It's still the stage, even if it is the coordinate space of a child object.

I suppose you could create a custom Event that would fire once your ChildClass1's constructor has finished its duties (one of which I guess is creating a ChildClass2 instance), that is listened to by the Document class, and upon detecting the Event it adds the object it receives to the stage. If you'd like me to illustrate how you'd do that I can go look through some code, but right now I can't imagine what advantage that'd even get you.

Anyway, hope at least some of that is of some help!

debu

debu
parent.addChild(ChildClass2instance) doesn't work for me. Flash throws the error "Access of undefined property parent". So far the only solution that works for me is passing the stage to ChildClass one and then calling stage.addChild().
Organiccat
Ok, but I still don't see why you can't just do addChild() from within the ChildClass1? That will definitely add your MovieClip to the stage, with no errors. Is there a reason why this method isn't good for you?
debu
I got an error about addChild() was not a recognized function.
Organiccat
Are you importing flash.display.* ? Never had addChild not be recognised, but I imagine a basic import like that being missing could cause it. What exactly is the error?
debu
And I mean doing the import inside your ChildClass1, not the Document class.
debu