我正在开发一款多点触控游戏,但是我似乎无法正常工作,此刻我在左下角有一个Dpad,当用户触摸一个Dpad按钮时,屏幕上的一个方形会移动,当他们释放他的手指时,它会停止,所以我在右下方添加了两个按钮,现在如果用户在Dpad上按住然后按下右边的一个底部,同时仍然保持,它将忘记手指向上并按下右侧的按钮,我如何才能支持多点触控事件?
这是我的代码的snipet,所以你可以看到我正在使用
this.addEventListener(Event.ENTER_FRAME,mainLoop);
//should make a game class
var _hero:Hero;
_hero = new Hero();
this.addChild(_hero);
_hero.init();
//variable for touch events
var touchOn:Boolean = false;
var touchX,touchY:Number;
//Direction buttons
var _DpadUp:Dpad_up; _DpadUp = new Dpad_up(); _DpadUp.init(0); this.addChild(_DpadUp);
var _DpadLeft:Dpad_Left; _DpadLeft = new Dpad_Left(); _DpadLeft.init(3); this.addChild(_DpadLeft);
var _DpadRight:Dpad_Right; _DpadRight = new Dpad_Right(); _DpadRight.init(1); this.addChild(_DpadRight);
var _DpadDown:Dpad_Down; _DpadDown = new Dpad_Down(); _DpadDown.init(2); this.addChild(_DpadDown);
var _DpadReset:Dpad_Down; _DpadReset = new Dpad_Down(); _DpadReset.init(4); this.addChild(_DpadReset);
var points:Object = new Object();
//Touch Events
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Constant update on move
this.stage.addEventListener (TouchEvent.TOUCH_BEGIN, onTouchBegin);
function onTouchBegin(e:TouchEvent):void{
//Touch Screen code
//touchOn = true;
touchX = e.stageX;
touchY = e.stageY;
}
//Constant update on move
this.stage.addEventListener (TouchEvent.TOUCH_MOVE, onTouchMove);
function onTouchMove(e:TouchEvent):void{
touchX = e.stageX;
touchY = e.stageY;
}
this.stage.addEventListener (TouchEvent.TOUCH_END, onTouchFinish);
function onTouchFinish(e:TouchEvent):void{
//touchOn = false;
touchX = -100;
touchY = -100;
}
//Main Loop
function mainLoop(e:Event):void {
if(_DpadUp.hitTestPoint(touchX,touchY,true)){_hero.y -=4;}else{};
if(_DpadRight.hitTestPoint(touchX,touchY,true)){_hero.x +=4;}else{};
if(_DpadDown.hitTestPoint(touchX,touchY,true)){_hero.y +=4;}else{};
if(_DpadLeft.hitTestPoint(touchX,touchY,true)){_hero.x -=4;}else{};
if(_DpadReset.hitTestPoint(touchX,touchY,true)){trace("RESET hit");}else{};
_hero.update();
}
任何帮助都会很棒
这是我想要的控制系统http://www.actionscript.org/forums/attachment.php3?attachmentid=38864&d=1352778655 附:我已经看过这个教程http://gotoandlearn.com/play.php?id=122,但是他将触摸ID链接到一个附有实例的对象,我只是想拥有它,所以当创建一个触摸点时,它将被包含在一个for循环中。检查是否触摸Dpad。
现在这是我的主循环代码
function mainLoop(e:Event):void {
for each(var p:Point in m_oTouchIDs)
{
trace(p);
if(_DpadUp.hitTestPoint(p.x,p.y,true)){_hero.y -=4;}else{};
if(_DpadRight.hitTestPoint(p.x,p.y,true)){_hero.x +=4;}else{};
if(_DpadDown.hitTestPoint(p.x,p.y,true)){_hero.y +=4;}else{};
if(_DpadLeft.hitTestPoint(p.x,p.y,true)){_hero.x -=4;}else{};
if(_DpadReset.hitTestPoint(p.x,p.y,true)){trace("RESET hit");}else{};
}
_hero.update();
}
当我按下一个按钮时没有任何事情发生,并且还有那条痕迹我得到了4次这样的痕迹
(x=27, y=12)
(x=27, y=12)
(x=27, y=12)
任何想法为什么?我发现,当我按下一个按钮时,它会出现在该对象上的位置,所以如果我点击该对象的左上角,它将返回x:1和y:1。我怎样才能测试碰撞?
我修复它:)而不是使用p.localX我使用p.stageX,这给了我触摸事件所在位置的X位置,使用local,它将在碰撞中的任何位置给出局部x位置用。
答案 0 :(得分:0)
你必须使用TouchEvent#touchPointID来识别触摸点,存储它并在循环中你可以对所有这些触点进行测试。
protected var m_oTouchIDs:Object = {};
protected function onTouchBegin(e:TouchEvent):void
{
m_oTouchIDs[e.touchPointID] = new Point(e.localX, e.localY);
}
protected function onTouchMove(e:TouchPoint):void
{
//update
var p:Point = m_oTouchIDs[e.touchPointID] as Point;
p.x = e.localX;
p.y = e.localY;
}
protected function onTouchEnd(e:TouchEvent):void
{
delete m_oTouchIDs[e.touchPointID];//release
}
protected function loop(e:Event):void
{
for each(var p:Point in m_oTouchIDs)
{
trace(p);//here you can compare touch position to execute some actions
}
/*
//or
for (var id:String in m_oTouchIDs)
{
m_oOutput.appendText("ID:" + id + " - " + m_oTouchIDs[id] + "\n");
}
*/
}