5

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

我期待/想要的是当我将手指放在按钮上时,播放器会继续移动,直到我停止触摸屏幕。

但发生的情况是,我必须不断地点击以保持玩家移动——而不是仅仅将手指放在按钮上让玩家继续移动。

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

4

1 回答 1

6

为此,您需要在两者之间连续运行一个函数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您的文档类,然后用适当的触摸事件替换您的 MouseEvent 侦听器:

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

于 2012-09-12T19:30:55.237 回答