简体   繁体   English

AS3:如何删除通过数组创建并放置在舞台上的MovieClip?

[英]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. LongSword是一个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". 现在,当LongSword被“出售”时,我试图从舞台上删除动画片段。 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. 如果数组中有多个Longsword实例,则您可能希望保留对每个实例的引用,以便进行更好的比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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