How can I define what a layer a child is created onto when using Flash CS5 AS3?

风流意气都作罢 提交于 2019-12-24 09:37:07

问题


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:


  1. Create a symbol in the library with nothing in it. I normally call these empty.
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!