Script using event listeners (touch) works in browser but not in simulator

Hello Everybody,

The following script (attached to a layout) works in the browser but not in the simulator, does someone have an explanation?
By the way, I read this topic on the forum, ([BUG] Touch events on mobile devices do not get sent, works on browsers though ) , could it be the same kind of bug (I havent yet tested my script on mobile devices) ?

   cc.Class({
        extends: cc.Component,
        properties: {               
        carreBleu: {default: null, type: cc.Node,},
        carreJaune: {default: null,type: cc.Node,},
        carreRouge: {default: null, type: cc.Node,},
        carreOrange: {default: null,type: cc.Node,},
        },
        // use this for initialization
        onLoad: function () {
            var listener1 = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
               onTouchBegan: function (touch, event) {      
                	var target = event.getCurrentTarget();
        	        var locationInNode = target.convertToNodeSpace(touch.getLocation());    
	                var s = target.getContentSize();
	                var rect = cc.rect(0, 0, s.width, s.height);
        	        cc.log(s.width);
	                if (cc.rectContainsPoint(rect, locationInNode)) {       
	                    target.opacity = 110;
        	            return true;
                	}
	                return false;
        		},       
               onTouchMoved: function (touch, event) { 
	                var target = event.getCurrentTarget();
        	        var delta = touch.getDelta();
                	target.x += delta.x;
	                target.y += delta.y;
            	},
               onTouchEnded: function (touch, event) {			
    		    	var target = event.getCurrentTarget();
	    		if(target.name="carreBleu"){  
        	           target.opacity=10;}
    		    	}
	        });
      cc.eventManager.addListener(listener1, this.carreBleu);
      cc.eventManager.addListener(listener1.clone(), this.carreJaune);
      cc.eventManager.addListener(listener1.clone(), this.carreRouge);
      cc.eventManager.addListener(listener1.clone(), this.carreOrange);
    },
        });

Which Cocos Creator version do you use?

Version 1.4.2

If you have time - and you are brave enough - try CC 1.5 beta 3.2, for me it solved a lot of different problems. Or use the node.on function to handle events, it is the preferred way.

Best regards,
Zsolt

1 Like

Thank you very much for the answer, I’m going to follow your advice.
Best regards, Jf

1 Like

For those who could be interested by the topic, here is the way I found :

    onLoad: function () {
     this.node.on('touchstart', function(touch,event) {
    var a = touch.getLocation();
    cc.log(a.x);
    });
    this.node.on('touchmove', function(touch,event) {
    var a = touch.getLocation();
    var delta = touch.getDelta();
    this.x += delta.x;
    this.y += delta.y;
    }); 

(script attached to the sprite in this example) + look here for more infos : http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/internal-events/

instead of the way described here :
(Get Touch Location or New event manager in Cocos2d-Html5 v3.0)

1 Like