Stage.addChild and layering

actionscript-3

I have a simple function that, when you click on a button, adds a movie clip to the stage in a random x and y position.

The issue I am having is that the new movie clips eventually cover up the button. I tried to change the z-index of the newly created movie clip to be below the z-index of the button, but that doesn't solve the problem.

How does one stop the new movie clips from covering up an element that already exists?

friends1_btn.addEventListener(MouseEvent.CLICK, friendMaker);

function friendMaker(evt:MouseEvent):void {
    var newFriend:Teddy = new Teddy();
    newFriend.x = Math.random()*stage.width;
    newFriend.y = Math.random()*stage.height;
    newFriend.z = 0;
    stage.addChild(newFriend);
}

Best Answer

Or alternatively - and maybe more use longterm - don't have all the added objects as children of the same layer as the button.

So instead of:

stage
  |
  +--- object 1
  |
  +--- object 2
  |
  +--- button
  |
  +--- object 3

Have:

stage
  |
  +--- object layer
  |       |
  |       +--- object 1
  |       |
  |       +--- object 2
  |
  +--- button

Or even:

stage
  |
  +--- object layer
  |       |
  |       +--- object 1
  |       |
  |       +--- object 2
  |
  +--- button layer
          |
          +--- button

Where object layer and button layer could simply be Sprite objects, i.e.:

var objectLayer:Sprite=new Sprite();
var buttonLayer:Sprite=new Sprite();
stage.addChild(objectLayer);
stage.addChild(buttonLayer);
buttonLayer.addChild(myButton);

and so on.

I think you'll find it's more useful to get into that way of thinking longterm rather than just to shift the z-indices around.

Incidentally, the updated Flash Player 10 does have a .z property (even though it's not in the documentation) - as Reuben says, it's used for the new 3D transforms. Sadly 3D transforms have no support whatsoever for z-sorting or z layering, so don't help in this case.

Related Topic