# keyDown is getting stuck with eventListener

**URL:** <https://forum.kirupa.com/t/keydown-is-getting-stuck-with-eventlistener/264141>\
**Category:** flash\
**Created:** [June 23, 2008, 3:08pm UTC](https://forum.kirupa.com/t/keydown-is-getting-stuck-with-eventlistener/264141 "2008-06-23T15:08:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cF516](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cF516](https://forum.kirupa.com/u/cF516)\
**Post date:** [June 23, 2008, 3:08pm UTC](https://forum.kirupa.com/t/keydown-is-getting-stuck-with-eventlistener/264141/1 "2008-06-23T15:08:15Z")

</div>

hey guys  
this is probably a simple solution but im making a car game and when u complete a level the listener is still sending information even though i stopped listening  
heres some code. can you help?

```auto
public function startRacing() {
						
			//place girls
			placeGirls();
			
			// make sure car is on top
			//gamesprite.setChildIndex(gamesprite.car,gamesprite.numChildren-1);
			
			// add listeners
			this.addEventListener(Event.ENTER_FRAME,gameLoop);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
			
			//set car loc
			gamesprite.car.x = -50;
			gamesprite.car.y = 842.0;
			gamesprite.car.rotation = -90;
			
			// set up game variables
			pointsText.text = String("0");
			level = 0;
			bounce = 1;
			score = 0;
			speed = 0;
			gameMode = "wait";
			timeDisplay.text = "";
			gameStartTime = getTimer()+3000;
			centerMap();
		}

```

start the game and initialize the listeners

```auto
// note key presses, set properties
		public function keyDownFunction(event:KeyboardEvent) {
			if (event.keyCode == 37) {
				arrowLeft = true;
			} else if (event.keyCode == 39) {
				arrowRight = true;
			} else if (event.keyCode == 38) {
				arrowUp = true;
			} else if (event.keyCode == 40) {
				arrowDown = true;
			}
		}
		
		public function keyUpFunction(event:KeyboardEvent) {
			if (event.keyCode == 37) {
				arrowLeft = false;
			} else if (event.keyCode == 39) {
				arrowRight = false;
			} else if (event.keyCode == 38) {
				arrowUp = false;
			} else if (event.keyCode == 40) {
				arrowDown = false;
			}
		}

```

recognize the buttons

```auto
// game over, remove listeners
		public function endGame() {
			this.removeEventListener(Event.ENTER_FRAME,gameLoop);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
			stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
			gotoAndStop("gameover");
		}

```

end the game and destroy the listeners

but when u hit the play again button  
the last arrows you pressed are still being sent so the car is moving in circles without any buttons down or anything

any idea?  
i can send the whole thing if you need more

thanks

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 23, 2008, 3:23pm UTC](https://forum.kirupa.com/t/keydown-is-getting-stuck-with-eventlistener/264141/2 "2008-06-23T15:23:37Z")

</div>

Somewhere you must have a function that handles the clicking of the PLAY AGAIN button.

What happens if you add the following line to that function:

```auto
stage.focus = this;

```

?

---

<div class="post-metadata">

**Author:** ![cF516](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cF516](https://forum.kirupa.com/u/cF516)\
**Post date:** [June 23, 2008, 4:23pm UTC](https://forum.kirupa.com/t/keydown-is-getting-stuck-with-eventlistener/264141/3 "2008-06-23T16:23:03Z")

</div>

it didnt work  
i was playing with the code and i commented out

```auto
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);

```

and it seemed to work

thanks anyway
