简体   繁体   中英

Add movieclip to stage from child class in Flash

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().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM