AS3按住按钮时连续运行代码 - 适用于iOS/Android的Air

use*_*767 5 air flash actionscript-3 touch ios

我正在使用Flash CS6开发iOS游戏.我有一个基本的运动测试,我把它放在一个Event.MOUSE_DOWN处理程序中.

我期待/想要的是当我用手指按下按钮时,玩家将一直移动直到我停止触摸屏幕.

但是,我必须不停地点击以保持玩家的移动 - 而不是仅仅按住按钮并且玩家继续移动.

我应该用什么代码来完成我想要的东西?

Bad*_*his 6

要实现这一点,您需要在两者之间连续运行一个函数MouseEvent.MOUSE_DOWN,Event.MOUSE_UP因为MouseEvent.MOUSE_DOWN每次按下只会调度一次.

这是一个简单的脚本:

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
}
Run Code Online (Sandbox Code Playgroud)

另外,您可能希望使用TOUCH事件,因为它为多点触控提供了更大的灵活性.虽然如果你只是允许在任何给定时间按下一个项目,这不是问题.

要做到这一点,只需添加Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT您的文档类,然后用适当的触摸事件替换您的MouseEvent侦听器:

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