0

Does anyone know how to move a movie clip by clicking a button on the stage. I can get it to move in increments, but I want it to move constantly. Currently I have this:

down.addEventListener(MouseEvent.MOUSE_DOWN, arrowDown);

function arrowDown(event:MouseEvent):void
{
bottomArrow.y += 1;
}
  • In your `arrowDown`, start a timer or use an ENTER_FRAME listener to make the changes to `.y`. In a MOUSE_UP handler, stop the timer or ENTER_FRAME. – sberry Mar 20 '13 at 20:26
  • http://stackoverflow.com/questions/12394877/as3-run-code-continuously-while-holding-a-button-down-air-for-ios-android/12395009#12395009 – BadFeelingAboutThis Mar 20 '13 at 22:19

1 Answers1

1

First, you should listen for KeyboardEvents 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;
}
duTr
  • 714
  • 5
  • 21