AS3运行code连续而按住一个按键 - 空气中的iOS / Android的 [英] AS3 Run code continuously while holding a Button down - Air For iOS/Android
问题描述
我正在开发在Flash CS6的的iOS游戏。
我有一个基本的运动测试,我把在 Event.MOUSE_DOWN
处理程序。
I am developing an iOS game in Flash CS6.
I have a basic movement test that I put in an Event.MOUSE_DOWN
handler.
我很期待/想是当我拿着我的手指向下按钮,玩家将继续前进,直到我停止触摸屏幕。
What I'm expecting/wanting is when I held my finger down on the button, that the player would keep moving until I stop touching the screen.
不过会发生什么,是我要保持不断的拍打,以保持玩家的移动 - 而不是仅仅握住我的手指上的按钮,玩家继续移动
What happens though, is I have to keep constantly tapping to keep the player moving - rather than just hold my finger on the button and the player keeps moving.
什么code,我应该用它来完成我想要的吗?
What code should I use to accomplish what I want?
推荐答案
要做到这一点,你需要不断地在两者之间的MouseEvent.MOUSE_DOWN
运行功能和 Event.MOUSE_UP
作为的MouseEvent.MOUSE_DOWN只会被派往每preSS一次。
To accomplish this, you'll need to run a function continuously in between MouseEvent.MOUSE_DOWN
and Event.MOUSE_UP
as MouseEvent.MOUSE_DOWN will only get dispatched one time per press.
下面是一个简单的脚本来做到这一点:
Here is a simple script to do just that:
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
}
顺便说一句,你可能要使用触摸事件,因为它提供了多点触摸控制更加灵活。但如果你只是以往任何时候都允许一个项目是pssed在任何给定的时间$ P $,这不是一个问题。
As an aside, you may want to use the TOUCH events, as it gives more flexibility with multi-touch control. Though if you're only ever going to allow one item to be pressed at any given time, it's not an issue.
要做到这一点,只需添加 Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT
在你的文档类,然后用适当的触摸事件替换的MouseEvents听众:
TO do that, just add Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT
in your document class, then replace your MouseEvent listeners with the appropriate touch event:
的MouseEvent.MOUSE_DOWN
变成了: TouchEvent.TOUCH_BEGIN
侦听MouseEvent.MOUSE_UP
变成了: TouchEvent.TOUCH_END
MouseEvent.MOUSE_DOWN
becomes: TouchEvent.TOUCH_BEGIN
MouseEvent.MOUSE_UP
becomes: TouchEvent.TOUCH_END
这篇关于AS3运行code连续而按住一个按键 - 空气中的iOS / Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!