0
votes

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?

2
Could you clarify something - this is currently moving your instances of groente1_MC every frame, and occasionally creating new instances. When you say you're going to the next frame, do you mean the next screen of your game? - shanethehat
When you loos the game. I have this line for an other object too, but then, after 5 hits. you will go back to the main screen to 'play' it again. unfortunately when going back to this frame. I am still seeing these objects (This code is in Frame3. And I am going back to Frame1) - user838097

2 Answers

1
votes

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

1
votes

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 = [];
}