我目前在Android上制作太空射击游戏(A.K.A:Hell Bullet)。所以我为2个操纵杆制作了一个基础类,我独立用于Move和Fire。现在我的代码工作,如果我只移动一个,但不是同时。所以我做了一些研究,找到了关于Multitouch的东西,我尝试了一些东西,没有什么能正常工作。
任何人都知道为什么?
Joystick.as (使用一次有效的mouseEvent):
package LP {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Joystick extends MovieClip {
private var _startX: Number = 0;
private var _startY: Number = 0;
private var _tension: Number = 0.3;
private var _xSpeed: Number = 0;
public var _isDragging: Boolean = false;
public var _angle: int;
private var _radius: int;
public var _amplitudeX: Number;
public var _amplitudeY: Number;
public function Joystick() {
this.addEventListener(Event.ADDED_TO_STAGE, stageInit);
}
private function stageInit(e: Event): void {
this.removeEventListener(Event.ADDED_TO_STAGE, stageInit);
_startX = x;
_startY = y;
_radius = background.width / 2
addEventListener(MouseEvent.MOUSE_DOWN, on_mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, on_mouseUp);
stage.addEventListener(Event.ENTER_FRAME, on_enterFrame);
}
protected function on_mouseDown(e: MouseEvent): void {
_isDragging = true;
}
protected function on_mouseUp(e: MouseEvent): void {
_isDragging = false;
}
protected function on_enterFrame(e: Event): void {
if (_isDragging) {
_angle = Math.atan2(root.mouseY - _startY, root.mouseX - _startX) / (Math.PI / 180);
rotation = _angle;
stick.rotation = -_angle;
stick.x = mouseX;
if (stick.x > _radius) {
stick.x = _radius;
}
} else {
_xSpeed = -stick.x * _tension;
stick.x += _xSpeed;
}
_amplitudeY = Math.sin(_angle * (Math.PI / 180)) * (stick.x / 8);
_amplitudeX = Math.cos(_angle * (Math.PI / 180)) * (stick.x / 8);
}
}
}
这是我尝试使用 MultitouchInputMode.GESTURE (没有工作,什么也没发生,没有错误)
package LP {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class Joystick extends MovieClip {
Multitouch.inputMode = MultitouchInputMode.GESTURE;
private var _startX: Number = 0;
private var _startY: Number = 0;
private var _tension: Number = 0.3;
private var _xSpeed: Number = 0;
public var _isDragging: Boolean = false;
public var _angle: int;
private var _radius: int;
public var _amplitudeX: Number;
public var _amplitudeY: Number;
public function Joystick() {
this.addEventListener(Event.ADDED_TO_STAGE, stageInit);
}
private function stageInit(e: Event): void {
this.removeEventListener(Event.ADDED_TO_STAGE, stageInit);
_startX = x;
_startY = y;
_radius = background.width / 2
addEventListener(TouchEvent.TOUCH_BEGIN, on_Touch);
stage.addEventListener(TouchEvent.TOUCH_END, on_TouchEnd);
stage.addEventListener(Event.ENTER_FRAME, on_enterFrame);
}
protected function on_Touch(e: TouchEvent): void {
_isDragging = true;
}
protected function on_TouchEnd(e: TouchEvent): void {
_isDragging = false;
}
protected function on_enterFrame(e: Event): void {
if (_isDragging) {
_angle = Math.atan2(root.mouseY - _startY, root.mouseX - _startX) / (Math.PI / 180);
rotation = _angle;
stick.rotation = -_angle;
stick.x = mouseX;
if (stick.x > _radius) {
stick.x = _radius;
}
} else {
_xSpeed = -stick.x * _tension;
stick.x += _xSpeed;
}
_amplitudeY = Math.sin(_angle * (Math.PI / 180)) * (stick.x / 8);
_amplitudeX = Math.cos(_angle * (Math.PI / 180)) * (stick.x / 8);
}
}
}
答案 0 :(得分:0)
您必须按照触摸事件的父坐标分隔您的操纵杆,包括TOUCH_DRAG
以更新您的操纵杆'内部立场。然后,如果注册了TOUCH_BEGIN
事件,则每个操纵杆应通过查询其自己的(x,y)坐标并与localToGlobal
结果进行比较来检查其坐标是否在该操纵杆区域中,或者e.stageX
和e.stageY
,如果不属于他们,请退出事件监听器。此外,您应该使用其他多点触控模式MultitouchInputMode.TOUCH_POINT
来实现此功能,因为只有此模式才会调度正确的触摸拖动/移动事件。一个例子:
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
private var _touchIndex:int;
private function stageInit(e: Event): void {
this.removeEventListener(Event.ADDED_TO_STAGE, stageInit);
_startX = x;
_startY = y;
_radius = background.width / 2
stage.addEventListener(TouchEvent.TOUCH_BEGIN, on_Touch);
stage.addEventListener(TouchEvent.TOUCH_END, on_TouchEnd);
stage.addEventListener(TouchEvent.TOUCH_DRAG, on_TouchDrag);
}
protected function on_Touch(e: TouchEvent): void {
var pe:Point=new Point(e.stageX,e.stageY);
var pc:Point=localToGlobal(new Point());
// what if joystick was moved past placing? Otherwise use center X and Y stored
// it's critical that these points must be in same coordinate system
if (Point.distance(pe,pc)<_radius) {
// this touch is ours, keep it
_isDragging=true;
// grab touch point ID
_touchIndex=e.touchPointID;
}
}
protected function on_TouchEnd(e: TouchEvent): void {
if (!_isDragging) return;
if (e.touchPointID==_touchIndex) {
// it is "our" touch that's been released
_isDragging = false;
}
}
protected function on_TouchDrag(e: TouchEvent): void {
if (!_isDragging) return;
if (e.touchPointID==_touchIndex) {
// it is "our" touch that's been dragged
// do your maths to update the joystick
// make sure you will use local coordinates to update parts of joystick!
}
}
并且不要忘记在Event.REMOVED_FROM_STAGE
听众中清除那些与舞台相关的听众,以获得更大的利益。
答案 1 :(得分:0)
使用Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
请勿使用TouchEvent.DRAG
,请使用TouchEvent.MOVE
。
你不应该像我之前所说的那样分开你的触摸事件,因为你有Joystick
两个不同的实例。