简体   繁体   中英

AS3: How to remove a MovieClip created and placed on stage through an array?

I'm making a game in flash and am using arrays to dynamically create items and place them inventory. LongSword is a MovieClip. I place the movieclip in the array like so:

function buyitem1(e:Event):void
{
    if(Store.itemslot.length < 6 && Store.currentMenu == 1 &&score >= 450)
    {
        Store.itemslot.push(new LongSword);
    }
}

Now I'm trying to remove the movieclip from the stage when the LongSword is "sold". How can i remove this longsword? I've tried:

for(var i:int = 0; i < Store.itemslot.length; i++)
{
    if(Store.itemslot[i] == LongSword)
    {
        stage.removeChild(Store.itemslot[0]);
    }
}

Ive also tried:

for(var i:int = 0; i < Store.itemslot.length; i++)
{
    if(Store.itemslot[i] == new LongSword)
    {
        stage.removeChild(Store.itemslot);
    }
}

and several variations. any ideas?

Try something like:

for each(var i:MovieClip in Store.itemslot)
{
    if(i is Longsword)
    {
        var n:int = Store.itemslot.indexOf(i);
        Store.itemslot.splice(n, 1);

        if(i.parent) i.parent.removeChild(i);

        break; // Only remove one Longsword.
    }
}

If there are multiple instances of Longsword in the array, you may want to keep a reference to each instance somewhere for better comparison.

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