簡體   English   中英

AS3:如何刪除通過數組創建並放置在舞台上的MovieClip?

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

我正在制作游戲,正在使用數組動態創建物品並將它們放置在庫存中。 LongSword是一個MovieClip。 我將動畫片段放置在數組中,如下所示:

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

現在,當LongSword被“出售”時,我試圖從舞台上刪除動畫片段。 如何刪除這把長劍? 我試過了:

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

我也嘗試過:

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

以及幾種變體。 有任何想法嗎?

嘗試類似的方法:

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.
    }
}

如果數組中有多個Longsword實例,則您可能希望保留對每個實例的引用,以便進行更好的比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM