Android As3中同时使用多个虚拟操纵杆
我目前正在android上制作一款太空射击游戏(又称地狱子弹)。因此,我为2个操纵杆创建了一个基类,用于独立移动和开火。现在,如果我一次只移动一个,而不是同时移动,我的代码就可以工作。所以我做了一些关于多点触控的研究和发现,我尝试了一些东西,但没有一个是正确的 有人知道为什么吗 操纵杆。如(带有一次工作一个鼠标事件): 这是我尝试的多点触摸输入模式。手势(不起作用,没有发生任何事情,也没有错误)Android As3中同时使用多个虚拟操纵杆,android,actionscript-3,flash,multi-touch,Android,Actionscript 3,Flash,Multi Touch,我目前正在android上制作一款太空射击游戏(又称地狱子弹)。因此,我为2个操纵杆创建了一个基类,用于独立移动和开火。现在,如果我一次只移动一个,而不是同时移动,我的代码就可以工作。所以我做了一些关于多点触控的研究和发现,我尝试了一些东西,但没有一个是正确的 有人知道为什么吗 操纵杆。如(带有一次工作一个鼠标事件): 这是我尝试的多点触摸输入模式。手势(不起作用,没有发生任何事情,也没有错误) 您必须按触摸事件的父坐标分离操纵手柄,包括触摸\拖动以更新操纵手柄的内部位置。然后,如果注册了TOU
您必须按触摸事件的父坐标分离操纵手柄,包括
触摸\拖动
以更新操纵手柄的内部位置。然后,如果注册了TOUCH\u BEGIN
事件,则每个操纵杆应通过查询自己的(x,y)坐标并与localToGlobal
结果或e.stageX
和e.stageY
进行比较来检查其坐标是否在该操纵杆的区域内,如果不在其范围内,则退出事件侦听器。此外,您应该使用另一种多点触摸模式,multi-touchinputmode.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!
}
}
multi-TOUCH.inputMode=multi-touchinputmode.TOUCH\u点;
私有var_指数:int;
私有函数阶段单元(e:事件):无效{
此.removeEventListener(Event.ADDED_到_STAGE,stageInit);
_startX=x;
_startY=y;
_半径=背景宽度/2
stage.addEventListener(TouchEvent.TOUCH\u BEGIN,on\u TOUCH);
stage.addEventListener(TouchEvent.TOUCH\u END,on\u TouchEnd);
stage.addEventListener(TouchEvent.TOUCH\u拖动,on\u TouchDrag);
}
触摸上的受保护功能(e:TouchEvent):无效{
var pe:点=新点(e.stageX,e.stageY);
var pc:Point=localToGlobal(new Point());
//如果操纵杆移过放置位置会怎样?否则使用存储的中心X和Y
//关键是这些点必须在同一坐标系中
如果(点距离(pe,pc)使用multi-TOUCH.inputMode=multi-touchinputmode.TOUCH\u点;
不要使用TouchEvent。拖动,使用TouchEvent.MOVE
你不需要像我前面的那个家伙说的那样分开你的触摸事件,因为你有两个不同的操纵杆
使用触摸点而不是手势正是我必须做的,谢谢!现在我必须考虑如何替换root.mouseX和root.mouseY,因为当我同时使用这两个按钮时,会导致冲突k因为它们都使用相同的coordonate.e.stageX和e.stageY可能对您非常有用。
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);
}
}
}
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!
}
}