AS3 Run code continuously while holding down button - Air For iOS / Android

I am developing an iOS game in Flash CS6. I have a basic motion test that I entered in a handler Event.MOUSE_DOWN.

What I expect / want is when I held my finger on the button so that the player continues to move until I touch the screen.

What is happening, I have to constantly monitor so that the player moves - instead of just holding the button with the finger, and the player continues to move.

What code should be used to accomplish what I want?

+5
source share
1 answer

To do this, you will need to continuously run the function between MouseEvent.MOUSE_DOWNand Event.MOUSE_UP, since MouseEvent.MOUSE_DOWN will be sent only once when pressed.

script, :

myButton.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);

function mouseDown(e:Event):void {
    stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
    addEventListener(Event.ENTER_FRAME,tick); //while the mouse is down, run the tick function once every frame as per the project frame rate
}

function mouseUp(e:Event):void {
    removeEventListener(Event.ENTER_FRAME,tick);  //stop running the tick function every frame now that the mouse is up
    stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}

function tick(e:Event):void {
    //do your movement
}

, TOUCH, . , - , .

, Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT , MouseEvent :

MouseEvent.MOUSE_DOWN : TouchEvent.TOUCH_BEGIN
MouseEvent.MOUSE_UP : TouchEvent.TOUCH_END

+6

All Articles