0

I'm working on a multi touch game but I cant seem to get something to work, at the moment I have a draw Dpad in the bottom left corner, when the user touches a Dpad button a sqaure on the screen will move, when they release thier finger it stops, so i added two buttons to the bottom right, now if the users is holding up on the Dpad and then presses one of the bottoms on the right, while still holding up, it will forget about the finger on up and press the button on the right, How can i get it to support multi touch events?

Here is a snipet of my code so you can see what im working with

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();
    }

Any help would be great

this is the control system I would like http://www.actionscript.org/forums/attachment.php3?attachmentid=38864&d=1352778655 P.S. I have looked at this tutorial http://gotoandlearn.com/play.php?id=122 but he links the touch IDs to a object which has a instance attached, i just want to have it so when a touch point is created, it will be included in a for loop around the check if touching a Dpad.

This is now my code for just the main loop

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();
}

When i press down on a button nothing happens, and also with that trace i get the trace outputed 4 times like so

(x=27, y=12)
(x=27, y=12)
(x=27, y=12)

any idea why? I have found out, when i press on a button it comes up with the position it is on that object, so if i click on the top left corner of the object, it would return x:1 and y:1. how can i test for collision?

I fixed it :) instead of using p.localX i use p.stageX, this gives me the X position on where ever the touch event is, with local, it will give the local x position on whatever it is in collision with.

Canvas
  • 5,779
  • 9
  • 55
  • 98

1 Answers1

0

You have to use TouchEvent#touchPointID to identify the touched point, store it and in your loop you can do hit test against all those touch points.

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");
    }
    */
}
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79