如何在ActionScript 3.0

时间:2017-05-29 14:00:20

标签: arrays actionscript-3 removechild

在我的一个框架中,我放置了一个带有movieclip随机帧的movieclip数组。将动画片段放置在某个帧后,它会从数组中拼接出来。在第45帧我想摆脱创建的实例,但我尝试使用cats.removeChild或其变体的所有东西似乎都不起作用。有人知道如何摆脱创建的实例吗?

    function Codetest():void {
        var cardlist:Array = new Array();
        for(var i:uint=0;i<4*1;i++) {
            cardlist.push(i);
        }

        for(var x:uint=0;x<4;x++) {
            for(var y:uint=0;y<1;y++) {
                var c:cats = new cats();
                c.x = x*450+105;
                c.y = y*550+300;
                var r:uint = Math.floor(Math.random()*cardlist.length);
                c.cardface = cardlist[r];
                cardlist.splice(r,1);
                c.gotoAndStop(c.cardface+1);

                addChild(c); // show the card
            }

        }

}

2 个答案:

答案 0 :(得分:3)

您需要以某种方式存储您创建的动画片段,例如在另一个数组中:

// this variable should be outside of your function so it will be still available at frame 45. Variables declared inside the function are only accessible from that function
var cardsArray:Array = []; // same as new Array() but a bit faster :)

function Codetest():void {
    var cardlist:Array = new Array();
    for(var i:uint=0;i<4*1;i++) {
        cardlist.push(i);
    }

    for(var x:uint=0;x<4;x++) {
        for(var y:uint=0;y<1;y++) {
            var c:cats = new cats();
            c.x = x*450+105;
            c.y = y*550+300;
            var r:uint = Math.floor(Math.random()*cardlist.length);
            c.cardface = cardlist[r];
            cardlist.splice(r,1);
            c.gotoAndStop(c.cardface+1);

            addChild(c); // show the card

            // push the new card movieclip into our fancy array
            cardsArray.push(c);
        }
    }
}

第45帧:

// loop trough our movieclips and remove them from stage
for(var i:uint=0; i < cardsArray.length; i++) 
{
    removeChild(cardsArray[i]);
}

// clear the array so the garbage collector can get rid of the movieclip instances and free up the memory
cardsArray = [];

答案 1 :(得分:2)

参考似乎是一个类,因此您无法通过 cats 类删除的实例(除非编码如此)。您需要做的是清理您将这些孩子添加到的容器。

实现:

function clear():void
{
    while (numChildren > 0) removeChildAt(0);
}

用法:

clear();