Event onTouchMoved : how to know which touch it refers to?

Event onTouchMoved : how to know which touch it refers to?
0.0 0



I’m trying to track multiple touches.

I received one onTouchBegan per new touch, but then how to know when onTouchMoved arrive, which touch the move refers to ?

Thanks in advance


   var listener = cc.EventListener.create({
    	swallowTouches: true,
    		if(this.sprite == null){
    			cc.log("Create sprite");	
    			this.sprite = cc.Sprite.create(res.CloseNormal_png);
    		this.sprite = null;
    	onTouchBegan: function(touch,event){
    		var target = event.getCurrentTarget();
    		var loc = touch.getLocation();
    		var loc = target.convertToNodeSpace(touch.getLocation());
    		cc.log("TOUCH BEGAN:"+loc.x+","+loc.y);
    		this.touchSpriteSetPos(loc.x, loc.y);
    		return true;
    	onTouchMoved: function(touch,event){
    		//cc.log("TOUCH MOVED");
    		var target = event.getCurrentTarget();
    		var loc = touch.getLocation();
    		var loc = target.convertToNodeSpace(touch.getLocation());
    		this.touchSpriteSetPos(loc.x, loc.y);
    	onTouchEnded: function(touch,event){
    		cc.log("TOUCH ENDED");
    		var target = event.getCurrentTarget();
    var self=this;


event->getCurrentTarget() returns the node, the event occurred on, aka the touch refers to.


or you can use this._node


Hi thanks for the fast answers.

Sorry my question was not completely clear (a bit late at night;-)

Yes getCurrentTarget gives me the node on which the touch event happened, but what I want to know is:
when I receive an onTouchMoved event, from which touchBegin it is the new position, so that I can follow between touch events.

Imagine I want to draw a different ball sprite under each fingers that moves and make that ball move under the finger (like painting tool)

I remember on iOS (I was using Cocos also) when I received touch move events they would contain a kind of ID of which touch they where the new position.

Here is an attached pic to make it maybe more clear:




Do you mean multi touch like this?

var touchListener = cc.EventListener.create({
        event: cc.EventListener.TOUCH_ALL_AT_ONCE,
        onTouchesBegan: function(touch,event){
            return true;
        onTouchesMoved: function(touch,event){
                var pos = touch[0].getLocationInView();
                cc.log("Touch 1 :" + pos.x +","+ pos.y);
                var pos = touch[0].getLocationInView();
                cc.log("Touch 2 :" + pos.x +","+ pos.y);

    cc.eventManager.addListener(touchListener, this);


But you also get the current target in your onTouchMoved event. This will be the referrer/target you are still touching/moving. onTouchMoved is just receiving touch events, as long as you keep your finger touched on the object.


looks interesting. will try tonight. Thanks
will report on it.


howlryu : thx. now I see what I missed : I used TOUCH_ALL_AT_ONCE but I did not see that the functions are onTouchESMoved instead of onTouchMoved :wink:

iQD: yes I could do that but when the new position is out of a node then I don’t get an event ?



Use the source, Luke! :wink:

Yes, would not get the event. But you cannot get out of a node, cause you are always touching the device and move along the sprite. You are only getting out of the node, if you don’t move along the sprite or lift your finger again :smiley:


Hi all,

I hope (and I think) that you had your reply since 2014… But since I just ask myself about this problem, this is the answer : you can use the informations stored in Touch object transmitted to callbacks : this Touch object had an ID member you can get with getID() method. And each touch began, move and ended can be identified since they will have the same ID.

I hope it will help somebody in the future !

See yaa