0

我正在开发一个多点触控游戏,但我似乎无法工作,目前我在左下角有一个绘图 Dpad,当用户触摸 Dpad 按钮时,屏幕上的一个正方形会移动,当他们松开手指它会停止,所以我在右下角添加了两个按钮,现在如果用户按住 Dpad 然后按下右侧的底部之一,同时仍然按住,它会忘记手指向上并按下右侧的按钮,我怎样才能让它支持多点触控事件?

这是我的代码片段,因此您可以看到我在使用什么

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 PS 我看过这个教程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 位置,对于本地,它会在与它发生碰撞的任何地方给出本地 x 位置。

4

1 回答 1

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");
    }
    */
}
于 2012-11-13T07:25:15.873 回答