# keyboardEventListener problem

**URL:** <https://forum.kirupa.com/t/keyboardeventlistener-problem/278825>\
**Category:** flash\
**Created:** [January 6, 2009, 12:12pm UTC](https://forum.kirupa.com/t/keyboardeventlistener-problem/278825 "2009-01-06T12:12:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grebs](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@grebs](https://forum.kirupa.com/u/grebs)\
**Post date:** [January 6, 2009, 12:12pm UTC](https://forum.kirupa.com/t/keyboardeventlistener-problem/278825/1 "2009-01-06T12:12:41Z")

</div>

i have built a space game. i am using numbers for the menu screen. when the user presses 1 i want 10 instances of a movieclip called blackhole to be added to the stage and when the user presses 2 i want 5 instances of the same movieclip to be added to the stage. This works as an easy and normal setting. However when the user presses 1 or 2 i get this error -

Error: Error #1023: Stack overflow occurred.  
at spaceGameMobileMenuWorkingPublished\_fla::MainTimeline/position()  
at spaceGameMobileMenuWorkingPublished\_fla::MainTimeline/position()  
at spaceGameMobileMenuWorkingPublished\_fla::MainTimeline/position() etc…

Here is my code -

```auto

stage.addEventListener(KeyboardEvent.KEY_DOWN, chooseStart);//adds an eventlistener to the stage to listen for a keyDown event
function chooseStart(evt:KeyboardEvent):void
{
    if(evt.keyCode == 49)//listens for the 1 key
    {
        openingSoundSC.stop();//plays the openingSound
        moon.visible = true;//shows the moon movieclip on the stage
        for(i = 0; i < 10; i++)//executes the for loop for 10 times
    {
        blackHole.x = Math.random() * stage.stageWidth;
        blackHole.y = Math.random() * stage.stageHeight;
        position(blackHole);
        blackHoles.push(blackHole);//stops blackholes overlapping
        blackHoles.push(moon);//stops blackholes and moon overlapping
        blackHoles.push(gameTimer_tb);//stops blackholes and gameTimer overlapping
        blackHoles.push(lives_tb);//stops blackholes and lives_tb overlapping
        blackHoles.push(spaceship_mc);//stops blackholes and spaceship_mc overlapping
        addChild(blackHole);//add the blackhole to the stage
    }
    gravity = 1;//sets the gravity to 1
    timer.start();//starts the timer
    addChild(spaceship);//adds the spaceship to the stage
    spaceship.x = 30;
    spaceship.y = 20;//positions spaceship
    removeChild(startButton);//removes startButton from stage
    removeChild(easy);//removes easy button from stage
    removeChild(hard);//removes hard button from stage
    removeChild(exit);//removes exit button from stage
    removeChild(splashScreen);//removes splashScreen from stage
    removeChild(instructionsText);//removes//instructions text from stage
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, chooseStart);//removes the start event listener
    } 
    else if(evt.keyCode == 50)//listens for the 2 key
    {
        moon.visible = true;
        for(i = 0; i < 5; i++)//executes the for loop for 5 times
    {
        blackHole.x = Math.random() * stage.stageWidth;
        blackHole.y = Math.random() * stage.stageHeight;
        position(blackHole);
        blackHoles.push(blackHole);
        blackHoles.push(moon);
        blackHoles.push(gameTimer_tb);
        blackHoles.push(lives_tb);
        blackHoles.push(spaceship_mc);
        addChild(blackHole);
    }
    gravity = 1;
    timer.start();
    addChild(spaceship);
    spaceship.x = 30;
    spaceship.y = 20;
    removeChild(instructionsText);
    removeChild(startButton);
    removeChild(easy);
    removeChild(hard);
    removeChild(exit);
    removeChild(splashScreen);
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, chooseStart);
    }
}

```

Can anyone help?
