问题
I need to display a child on layer 2. How would I, using AS3, dynamically create a child on frame 2?
回答1:
"Layer 2" isn't specific enough. What layer of what display object container?
Here is a good article on display list programming that should prove enlightening.
To answer your question, you would identify the parent displayObject and call its addChild method, with your targeted child displayobject as the parameter. If your parent displayObject is the containing class (a class that extends DisplayObject, a Sprite, for example), you can just call addChild() or this.addChild(). To add the child at a layer other than the topmost, you can use addChildAt().
var someclip:Sprite = new Sprite();
var someOtherClip:Sprite = new Sprite();
var yetAnotherClip:Sprite = new Sprite();
var someLibrayClip:LibraryClip = new LibraryClip();
this.addChild(someClip);
this.addChildAt(someOtherClip,0);
someOtherClip.addChild(yetAnotherClip);
someOtherClip.addChildAt(someLibrayClip,0);
etc...
Note that the display list is a stack like an array, and in its case, may not contain empty indexes. If you want something in layer 2, there must also be items in 0 and 1.
Hope that helps -
回答2:
Layers only exist in the Flash IDE. They are not part of Flash Player's display list system. So you can't specify what layer a child goes into. Use addChild() or addChildAt() to add children to containers.
回答3:
- Create a symbol in the library with nothing in it. I normally call these empty.
- Place an instance of this symbol on stage where you need it. Give the instance a name. For sake of this answer, it is empty_mc.
- Then in AS do
var whatever : Whatever = new Whatever(); empty_mc.addChild(whatever);
at the appropriate place.
来源:https://stackoverflow.com/questions/5480670/how-can-i-define-what-a-layer-a-child-is-created-onto-when-using-flash-cs5-as3