Search code examples
actionscript-3flashbuttonmovieclip

Actionscript 3: Buttons always on top?


I'm working on a little game. So I need 4 layers in my main-timeline. In the bottom layer is a movieclip with some buttons in them. In the two layers above there are also movieclips for effects and actions and the layer on the top is for actionscript. The sequence for the layers is intended.

And when I test it I can't click on any buttons, because for sure there are movieclips above them. Is there a possibility in actionscript to say, that all buttons are always on top?


Solution

  • You can also make a call to addChild each time a new DisplayObject / DisplayObjectContainer is added over one of your buttons...

    If your buttons are stored in a Vector as this:

    var buttons:Vector.<SimpleButton> = new Vector.<SimpleButton>();
    

    and you've a function like setButtonsOnTop

    function setButtonsOnTop():void{
        for(var i:uint=0;i<buttons.length;i++){
            addChild(buttons[i]);
        }
    }
    

    Call the function when something is added over the buttons.

    setButtonsOnTop();
    

    So the buttons are always above the other DisplayObjects...

    To remove the buttons you may do this by adding a "removeButtons" method:

    function removeButtons():void{
        for(var i:uint=0;i<buttons.length;i++){
            removeChild(buttons[i]);
        }
    }
    

    So in this way you don't have to care about the index value.