Kheva Mann
Kheva Mann

Reputation: 404

Add Child Behind existing object AS3

I have made a game using Flash CS5 (as3) and I am trying to add a child to the stage behind objects that are already there. For example, I have a bar at the bottom of the screen that is there from the time you start the game and I have falling objects that I want to fall behind it, but instead they fall in front of it because I added them to the stage after. Is there a way of adding the falling objects behind it without having to keep re-adding the bar to the stage? Thanks in advance.

Upvotes: 0

Views: 1994

Answers (2)

Velocirooster
Velocirooster

Reputation: 646

Rather than layering, I'd use adjust the index of each object using addChildAt and setChildIndex.

The following line adds your falling object behind every other DisplayObject on the stage (in this case, you should probably add your bar to the stage first)

stage.addChildAt(fallingObject, 0);

Upvotes: 2

Individual11
Individual11

Reputation: 394

You can create Sprites to act as layers, and add the different objects to them. Here is an example that adds a layer for adding whatever you want behind the bar layer, and THEN adds the bar layer, so it will be on top. It's super rough since you don't have any code to reference:

package  {
    import flash.display.*;
    public class Test extends MovieClip {

        var barLayer:Sprite;
        var objectLayer:Sprite;

        public function Test() {

            var objectLayer = new Sprite();
            //add the object layer to your main Class
            this.addChild(objectLayer);

            //now you can add movie clips or sprites to objectLayer whenever you like

            //then create the bar layer
            var barLayer = new Sprite();

            //add your bar Display Object here
            var bar = new MovieClip();//draw the bar or reference the library object you've created
            barLayer.addChild(bar);

            //now add the barLayer
            this.addChild(barLayer);        
        }
    }
}

Upvotes: 0

Related Questions