简体   繁体   中英

Remove multiple sprites from stage AS3

I've created a list of sprites (to hold textfields), how would I remove all the created sprites?

creating the sprites:

    for (var i:int = 0; i < optionsArray[currentChoicePart].length; i++) 
    {
        var txt:TextField = new TextField();
        txt.defaultTextFormat = new TextFormat('Verdana',15,0xFFFFFF);
        txt.text = optionsArray[currentChoicePart][i];
        txt.filters = [stroke];
        txt.autoSize = TextFieldAutoSize.LEFT;
        txt.selectable = false;
        txt.width = 400
        txt.height = 25
        var btn:Sprite = new Sprite();
        btn.mouseChildren = false;
        btn.addChild(txt); 
        btn.buttonMode = true;
        btn.x = stage.stageWidth / 10
        btn.y = stage.stageHeight / 2 - 50 * (i * .5)
        btn.name = "p" + String((Number(currentPart.substring(1)) + (i+1)))
        stage.addChild(btn)
        btn.addEventListener(MouseEvent.CLICK, function m(zen:MouseEvent) // when button is clicked 
        {
            choice(zen.currentTarget.name)

        } 
        )
    }

The entire approach is all wrong and your code the way it is written is only producing more headaches for you than it really should. You only need a special way to remove those buttons because you create that situation for yourself. In reality a more simple and more efficient code would be to have your button set in a specific sprite and then simply remove all buttons from that sprite like:

var buttonHolder:Sprite = new Sprite();
stage.addChild(buttonHolder);

//for loop
for (var i:int = 0; i < optionsArray[currentChoicePart].length; i++) 
//etc ... 
buttonHolder.addChild(btn);
//here the button is added to his specific holder

No removing button becomes very easy

buttonHolder.removeChildren();

Adding everything to the stage directly is a clue of inexperience for a coder. Nothing really should ever be added to the stage. Instead from your document class you should add more layers (Sprite) and add to those layers depending on your needs. You end up with a very easy to manage display list and set of layers. Adding to the stage will always make your work and logic more difficult and full of headaches down the road.

EDIT:

Looping through a display list or an array in order to remove or add from it is not recommended because it's tricky and can easily produce error. If you add or remove then the number of element changes and the loop which relies on that number might end up with weird results. If you remove then the loop will terminate too soon missing some element to remove. Instead typically this is done with a while loop to avoid this problem.

while(buttonHolder.numChildren)
{
    buttonHolder.removeChildAt(0);
}

Also the removeChildren method must be called like this

removeChildren();

not like this:

removeChildren;//missing ()

In a nutshel, I would recommend to save links to the buttons somewhere (eg Vector.) and just go through the list when you need to remove all buttons.

Try this code (please, pay attention to the comments):

import flash.display.Sprite;

var btns:Vector. = new Vector.();

function init():void
{
    trace("init");

    for (var i:int = 0; i < optionsArray[currentChoicePart].length; i++) 
    {
        var txt:TextField = new TextField();
        txt.defaultTextFormat = new TextFormat('Verdana',15,0xFFFFFF);
        txt.text = optionsArray[currentChoicePart][i];
        txt.filters = [stroke];
        txt.autoSize = TextFieldAutoSize.LEFT;
        txt.selectable = false;
        txt.width = 400
        txt.height = 25
        var btn:Sprite = new Sprite();
        btn.mouseChildren = false;
        btn.addChild(txt); 
        btn.buttonMode = true;
        btn.x = stage.stageWidth / 10
        btn.y = stage.stageHeight / 2 - 50 * (i * .5)
        btn.name = "p" + String((Number(currentPart.substring(1)) + (i+1)))
        stage.addChild(btn)
        btn.addEventListener(
            MouseEvent.CLICK, 
            function m(zen:MouseEvent) // when button is clicked 
            {
                choice(zen.currentTarget.name);
            } 
        )

        // Each button is put in the list,
        // to be able to work with the button after
        btns.push(btn);
    }

    trace(btns.length);
}

function reset():void
{
    trace("reset");

    var tempBtn:Sprite;
    var btnsCount:int = btns.length;
    // We just go through the list and remove all buttons from theirs parents
    for(var btnIndex:int = 0; btnIndex < btnsCount; btnIndex++)
    {
        tempBtn = btns[btnIndex];
        tempBtn.parent.removeChild(tempBtn);
    }

    btns = new Vector.<Sprite>();
}

init();
setTimeout(reset, 3000);

Also, I've tested the code above with commented lines where I don't have variables such as optionsArray, stroke, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM