# Move Object with Keyboard question

**URL:** <https://forum.kirupa.com/t/move-object-with-keyboard-question/263581>\
**Category:** Uncategorized\
**Created:** [June 17, 2008, 1:10pm UTC](https://forum.kirupa.com/t/move-object-with-keyboard-question/263581 "2008-06-17T13:10:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [June 17, 2008, 1:10pm UTC](https://forum.kirupa.com/t/move-object-with-keyboard-question/263581/1 "2008-06-17T13:10:37Z")

</div>

Hello,

I know how to use the four arrow keys to move a ball around. Here’s the codes:

> 

import flash.events.KeyboardEvent;

function moveBall(event:KeyboardEvent):void {  
if (event.keyCode == 39) {  
ball\_mc.x += 1;  
} else if (event.keyCode == 37) {  
ball\_mc.x -= 1;  
} else if (event.keyCode == 38) {  
ball\_mc.y -= 1;  
} else if (event.keyCode == 40) {  
ball\_mc.y += 1;  
}

}

stage.addEventListener(KeyboardEvent.KEY\_DOWN, moveBall);

The ball move a pixel when I press down the keys. I wish that when I press an arrow key (e.g. right arrow key), the ball will keep on moving right until I hit another arrow key (e.g. left arrow key). Then the ball will change moving in other direction (e.g. left).

I try to add a ENTER\_FRAME event inside the moveBall function:  
stage.addEventListener(Event.ENTER\_FRAME, moveBall);  
However it is not working and an error appear.

How can I do that?

Thanks and best regards

Alex

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 17, 2008, 1:18pm UTC](https://forum.kirupa.com/t/move-object-with-keyboard-question/263581/2 "2008-06-17T13:18:06Z")

</div>

The moveBall method can only work when a KeyboardEvent is fired at it, but you’re on the right track with an enter frame listener. You need to combine the two.

Instead of having moveBall change the x / y position of ball\_mc, have it set a variable ‘direction’ for example, e.g. direction = ‘up’;

Then have an enter frame function that uses that variable, probably with a switch. For example:

```auto

switch (direction)
{
     case 'up':
          ball_mc.y -= 1;
          break;

     case 'down':
          //etc...
}

```

---

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [June 18, 2008, 4:52am UTC](https://forum.kirupa.com/t/move-object-with-keyboard-question/263581/3 "2008-06-18T04:52:56Z")

</div>

Hello,

Thanks for your help. It is working now. Since I am new to Actionscript 3, I am not sure if this is the correct way to do it. Here’s my codes:

> 

import flash.events.KeyboardEvent;

var direction:String;

function detectKey(event:KeyboardEvent):String {  
if (event.keyCode == 39) {  
direction = “right”;  
} else if (event.keyCode == 37) {  
direction = “left”;  
} else if (event.keyCode == 38) {  
direction = “up”;  
} else if (event.keyCode == 40) {  
direction = “down”;  
} else if (event.keyCode == 32) {  
direction = “stop”;  
}

return direction;

}

function moveBall(evt:Event) {  
switch (direction) {  
case ‘up’ :  
ball\_mc.y -= 1;  
break;

case ‘down’ :  
ball\_mc.y += 1;  
break;

case ‘right’ :  
ball\_mc.x += 1;  
break;

case ‘left’ :  
ball\_mc.x -= 1;  
break;

case ‘stop’ :  
// ENTER\_FRAME event cannot be triggered again if add removeEventListener here  
[//stage.removeEventListener](https://stage.removeEventListener)(Event.ENTER\_FRAME, moveBall);  
break;  
}  
}

stage.addEventListener(KeyboardEvent.KEY\_DOWN, detectKey);

stage.addEventListener(Event.ENTER\_FRAME, moveBall);

Please check if the above codes can be improved.

It seems that the ENTER\_FRAME event is running all the time. I am afraid that it will consume a lot of computer resources.

Is it possible that the ENTER\_FRAME event is only triggered when the arrow keys are pressed. And the ENTER\_FRAME event will be removed when the space bar is pressed.

Thanks and best regards

Alex
