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

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

#1

Hi,

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

Nicolas

   var listener = cc.EventListener.create({
    	event:cc.EventListener.TOUCH_ONE_BY_ONE,
    	swallowTouches: true,
    	sprite:null,
    	
    	touchSpriteCreateIfNull:function(in_target){
    		if(this.sprite == null){
    			cc.log("Create sprite");	
    			this.sprite = cc.Sprite.create(res.CloseNormal_png);
    			in_target.addChild(this.sprite);
    		}
    	},
    	touchSpriteSetPos:function(in_x,in_y){
    		this.sprite.setPosition(in_x,in_y);
    	},
    	touchSpriteRemove:function(in_target){
    		in_target.removeChild(this.sprite);
    		this.sprite = null;
    	},
    	
    	onTouchBegan: function(touch,event){
    		var target = event.getCurrentTarget();
    		var loc = touch.getLocation();
    		cc.log("Raw:"+loc.x+","+loc.y);
    		var loc = target.convertToNodeSpace(touch.getLocation());
    		cc.log("TOUCH BEGAN:"+loc.x+","+loc.y);
    		this.touchSpriteCreateIfNull(target);
    		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();
    		this.touchSpriteRemove(target);
    		
    	}
    });
    var self=this;
    cc.eventManager.addListener(listener,self);

#2

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


#3

or you can use this._node


#4

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:

Thanks

Nicolas


#5

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){
            if(touch[0]){
                var pos = touch[0].getLocationInView();
                cc.log("Touch 1 :" + pos.x +","+ pos.y);
            }
            if(touch[1]){
                var pos = touch[0].getLocationInView();
                cc.log("Touch 2 :" + pos.x +","+ pos.y);
            }

        }
    },this);
    cc.eventManager.addListener(touchListener, this);

#6

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.


#7

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


#8

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 ?

Nicolas


#9

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:


#10

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

Olivier.