ActionScript 3.0在转到下一帧后删除数组/ chillds

时间:2011-11-10 16:28:26

标签: flash actionscript-3 adobe

我有什么:

var ster:Array;
ster = new Array();

stage.addEventListener(Event.ENTER_FRAME, groente);

function groente(event:Event)
{
    if(Math.round(Math.random()*140) == 1)
    {
            ster.push(new groente1_MC());
            addChild(ster[ster.length-1]);
            ster[ster.length-1].x = machine.x
            ster[ster.length-1].y = machine.y 
            ster[ster.length-1].scaleX = 0.2;
            ster[ster.length-1].scaleY = 0.2;
    }
    for(var i:int = 0; i < ster.length-1; i++)
    {
            ster[i].y = ster[i].y + 5;
            if( hero.hitTestObject(ster[i]) ) {
                ster[i].visible = false;
                score = score +1;
                Score.text = ""+ score;

            }
    }
}   

现在的问题是,在进入下一帧之后。我仍然可以看到groente1_MC。在我的舞台上还有更多的产卵。如何删除所有这些并在转到下一帧后停止产生它们?

2 个答案:

答案 0 :(得分:2)

您需要删除ENTER_FRAME侦听器以停止对象的移动和创建,然后循环通过数组从清除数组之前从舞台中删除每个对象:

function clearStage():void
{
    stage.removeEventListener(Event.ENTER_FRAME, groente);
    for each(var mc:groente1_MC in ster) {
        removeChild(mc);
    }
    ster = [];
}

答案 1 :(得分:1)

for(var i:int = ster.length -1; i >= 0; i--)
{
        ster[i].y = ster[i].y + 5;
        if( hero.hitTestObject(ster[i]) ) {
            ster[i].visible = false;
            score = score +1;
            Score.text = ""+ score;
            removeChild(ster[i]);
            ster.splice(i, 1);
        }
}

示例:

            var ar:Array = [0,1,2,3,4,5,6,7,8,9,10]
            for(var i:int = ar.length - 1; i >= 0; i--){
                if(i % 2 == 0){                        
                    tf.text +=  i + ' : ' + ar[i] + '\n';                    
                    ar.splice(i,1);
                }
            }
            tf.text += 'so : ' + ar.join(', ');

tf.text

  

10:10
8:8
6:6
4:4 2:2 0:0   
所以:1,3,5,7,9