Stage.addChild and bundle

I have a simple function that, when I click on the button, adds a video clip to the scene at a random position x and y. The problem I am facing is that the new clips end up covering the button. I tried changing the z-index of the newly created mc so that it is below the z-index of the button, but this does not solve the problem. How to stop a new mc from covering 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);
}
+3
source share
6 answers

Or, alternatively, and perhaps more use longterm - not all added objects as children of the same layer as the button.

So, instead of:

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

There is:

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

Or even:

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

object layer button layer Sprite , :

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

..

, , z- .

, Flash Player 10 .z ( ), , 3D-. 3D- z- z-, .

+12

, , addChildAt(), , DisplayObject, numChildren , .

private var friends1_btn:Sprite;

function friendMaker(evt:MouseEvent):void
{
    var newFriend:Teddy = new Teddy();
    newFriend.x = Math.random()*stage.width;
    newFriend.y = Math.random()*stage.height;
    stage.addChildAt(newFriend, stage.numChildren-1);
    //or if your not sure if the button will always be on top
    stage.addChildAt(newFriend, stage.getChildIndex(friends1_btn)-1);
}

, Event.ADDED_TO_STAGE, , - , ,

+2

MovieClip 'z' AS3 ()

display list AS3. , .

0

swapChildren() .

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;
    stage.addChild(newFriend);
    stage.swapChildren(newFriend, friends1_btn);
}
0

, - , . addChild , ( ), .

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;

        stage.addChild(newFriend);
        stage.addChild(friends1_btn);
    }

,

stage.addChild(friends1_btn);

stage.swapChildrenAt(stage.getChildIndex(friends1_btn), stage.numChildren - 1);
0

The "z" property for MovieClip is used for 3D transformations in Flash Player 10 - instead, you need to explicitly specify a child index MovieClip

A good way is to manually set the index friends1_btnto the foreground:

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;
    stage.addChild(newFriend);
    stage.setChildIndex(friends1_btn,stage.numChildren-1);
}

(Note: if you want to accidentally place MovieClip in the workspace, you must use stage.stageWidthand stage.stageHeight, otherwise, you will only get the width and height of the objects in the scene, not the scene itself).

0
source

Source: https://habr.com/ru/post/1718936/


All Articles