开发者

How do i remove multiple MC's from the stage in AS3

开发者 https://www.devze.com 2023-03-29 02:24 出处:网络
I create multiple MC\'s with the following code: function addCharacter() { var newCharacter:characterBob = new characterBob();

I create multiple MC's with the following code:

function addCharacter() {
    var newCharacter:characterBob = new characterBob();
    this.addChild(newCharacter);

    newCharacter.x=1000 - (50*counter);
    newCh开发者_Go百科aracter.y=50;

    counter = counter + 1
}

Now i'd like to remove a bunch of them from the stage. Is there a way i can do this in AS3?

Thanks in advance for any advice.


You can try something like:

for each (var o:DisplayObject in this) {
    if (o is characterBob) {
        removeChild(o);
    }
}

Perhaps a better option would be to put each "characterBob" created into an array. Then loop through the array and remove each object.

var bobs:Array = new Array();

function addCharacter() {
    var newCharacter:characterBob = new characterBob();
    this.addChild(newCharacter);

    newCharacter.x=1000 - (50*counter);
    newCharacter.y=50;

    counter = counter + 1;

    bobs.push(newCharacter);
}

function removeAllBobs():void {
    while (bobs.length > 0) {
        removeChild(bobs.shift());
    }
}


while(myDisplayObjectContainer.numChildren > 0){
    myDisplayObjectContainer.removeChildAt(0);
}

That will remove all children of the specified DisplayObjectContainer (Sprite or Movieclip)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号