Search code examples
actionscript-3flashflash-cs5addchild

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.


Solution

  • 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);        
            }
        }
    }