First, you should listen for KeyboardEvent
s instead of MouseEvent
. Then I think you should listen for those events being dispatched by the stage
.
Here is an example using the Event.ENTER_FRAME
event. If you'd like to control better the speed of you sprite moves you might want to user a timer instead.
This example works when the down arrow is pressed but you can change Keyboard.DOWN
with any key you want.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onEnterFrame(event:Event):void
{
bottomArrow.y += 1;
}