ActionScript 3.0 在进入下一帧后删除数组/childs

发布于 2024-12-14 19:13:19 字数 855 浏览 1 评论 0原文

我的情况是:

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。而且在我的舞台上还有更多的产卵。我怎样才能删除所有这些并在进入下一帧后停止生成它们?

What I have this:

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;

            }
    }
}   

Now the problem is, after going to the next frame. I can still see the groente1_MC. And there are still more spawning in my stage. How can i delete all these and stop spawning them after going to the next frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱*していゐ 2024-12-21 19:13:19

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

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

You need to remove the ENTER_FRAME listener to stop the movement and creation of objects, then loop though the array removing each object from the stage before clearing the array:

function clearStage():void
{
    stage.removeEventListener(Event.ENTER_FRAME, groente);
    for each(var mc:groente1_MC in ster) {
        removeChild(mc);
    }
    ster = [];
}
昨迟人 2024-12-21 19:13:19

尝试

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

try

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);
        }
}

example:

            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 is

10 : 10
8 : 8
6 : 6
4 : 4
2 : 2
0 : 0

so : 1, 3, 5, 7, 9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文