Neverlax
Neverlax

Reputation: 425

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?

Upvotes: 1

Views: 1370

Answers (1)

Marty
Marty

Reputation: 39456

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.

Upvotes: 2

Related Questions