Noah R
Noah R

Reputation: 5477

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

I need to display a child on layer 2. How would I, using AS3, dynamically create a child on frame 2?

Upvotes: 1

Views: 8375

Answers (3)

Bosworth99
Bosworth99

Reputation: 4234

"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 -

Upvotes: 3

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.

Upvotes: 1

mpdonadio
mpdonadio

Reputation: 2941

  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.

Upvotes: 0

Related Questions