Help Removing Event Listeners & Resetting Game

I’m trying to develop a catching game, just for a bit of fun and learning experience. I’ve been bouncing between tutorials and I’ve finally come to a point where I can’t get any further.

After you catch three sandwhiches, you lose and it takes you to an end screen, however, I’m unable to stop the listeners from being called, and when the game restarts, the food falls, but you’re no longer able to catch it or keep score.

As you can see in the code below, I tried


    while(numChildren>0)
    {
    getChildAt(0).removeEventListener(TimerEvent.TIMER_COMPLETE, newFood);
    removeChild(0);
    }

and it didn’t work. Can I have a little help? I’d hate to have gotten this far and scrap the entire project. :frowning:

Here is the code from the main timeline.


import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;
import flash.text.*;

stop();

// CREATES A BOUNDARY ON THE STAGE THAT STEFFEH CANNOT CROSS

function stageBoundary(object:MovieClip)
{
    var halfWidth:uint=object.width/2;
    
    if (object.x+halfWidth>stage.stageWidth)
    {
    object.x=stage.stageWidth-halfWidth;
    }
    else if (object.x - halfWidth <0) 
    {
    object.x=0+halfWidth;
    }
}

// GETTING STEFFEH TO MOVE WITH L AND R KEYS

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveSteffeh);

function moveSteffeh(e:KeyboardEvent):void 
{
    stageBoundary(steffeh);
    
    switch (e.keyCode) 
    {
    case Keyboard.LEFT :
    steffeh.x -=8;
    break;

    case Keyboard.RIGHT :
    steffeh.x +=8;
    break;
    }
} 

// SETTING UP FALLING FOOD

    var nextFood:Timer;
    var food:Array = new Array();
    const speed:Number = 7.5;

    setNextFood();
    addEventListener(Event.ENTER_FRAME, moveFood);

    function setNextFood()
    {
    nextFood = new Timer(1000+Math.random()*1000,1);
    nextFood.addEventListener(TimerEvent.TIMER_COMPLETE, newFood);
    nextFood.start();
    }

// GENERATES A NEW FOOD

        function newFood(e:Event)
        {
        var goodFood:Array = ["broccoli"];
        var badFood:Array = ["sammich"];
        if (Math.random() < .3) 
        {
        var r:int = Math.floor(Math.random()*goodFood.length);
        var classRef:Class = getDefinitionByName(goodFood[r]) as Class;
        var newFood:MovieClip = new classRef();
        newFood.typestr = "good";
        } else {
                r = Math.floor(Math.random()*badFood.length);
                classRef = getDefinitionByName(badFood[r]) as Class;
                newFood = new classRef();
                newFood.typestr = "bad";
                }
        newFood.x = Math.random()*500;
        addChild(newFood);
        food.push(newFood);
        setNextFood();
        }

// MAKE THE FOOD FALL

            function moveFood(e:Event)
            {
            for(var i:int=food.length-1; i>=0; i--)
                {
                food*.y += speed;
                if (food*.y > 400)
                    {
                    removeChild(food*);
                    food.splice(i,1);
                    }
                    
                    // CATCHING FOOD

                        if (food*.hitTestObject(steffeh))
                        {
                            if (food*.typestr == "good")
                            { 
                            score += 15;
                            } else
                                {
                                score -= 10;
                                eaten += 1;
                                }
                                
                        if(eatenDisplay.text=="2")
                        {
                            endGame();
                        }
                        
                        scoreDisplay.text = "" +score;
                        eatenDisplay.text = "" +eaten;
                        removeChild(food*);
                        food.splice(i,1);
                        }
                }
            }
// SCORE VARIABLES

var score:int = 0;
var eaten:int = 0;

// END SCREEN

function endGame()
{
    while(numChildren>0)
    {
    getChildAt(0).removeEventListener(TimerEvent.TIMER_COMPLETE, newFood);
    removeChild(0);
    }

removeChild(steffeh);
gotoAndStop("end");
}