AS3持续运行代码,同时按住button – Air For iOS / Android

我正在Flash CS6中开发iOS游戏。 我有一个基本的运动testing,我把一个Event.MOUSE_DOWN处理程序。

我期望/想要的是当我将手指按在button上时,玩家将继续移动,直到我停止触摸屏幕。

但是,为了保持玩家的移动,我必须不断地敲击,而不是只是把手指放在button上,玩家不停地移动。

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

为了达到这个目的,你需要在MouseEvent.MOUSE_DOWNEvent.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 } 

顺便说一下,您可能想要使用TOUCH事件,因为它为多点触控提供了更多的灵活性。 虽然如果你只允许在某个特定的时间允许一个项目被按下,这不是一个问题。

为此,只需在文档类中添加Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT ,然后用适当的触摸事件replaceMouseEvent侦听器:

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