Removing a component from the display list

You can remove a component from a display object container and its display list with the removeChild() and removeChildAt() methods. The following example places three Button components in front of each other on the Stage and adds an event listener for each of them. When you click each Button, the event handler removes it from the display list and the Stage.

To remove a component from the display list:

  1. Create a new Flash file (ActionScript 3.0) document.
  2. Drag a Button from the Components panel to the Library panel.
  3. Open the Actions panel, select Frame 1 in the main Timeline and add the following code:
    import fl.controls.Button;
    
    var i:int = 0;
    while(i++ < 3) {
        makeButton(i);
    }
    function removeButton(event:MouseEvent):void {
        removeChildAt(numChildren -1);
    }
    function makeButton(num) {
        var aButton:Button = new Button();
        aButton.name = "Button" + num;
        aButton.label = aButton.name;
        aButton.move(200, 200);
        addChild(aButton);
        aButton.addEventListener(MouseEvent.CLICK, removeButton);
    }
    

For a complete explanation of the display list, see Display programming in Programming ActionScript 3.0.