Cocos2d-x-3.0 Touch事件处理机制

在学习Cocos2d-html5游戏例子的时候,注册事件代码一直提示:TypeError: cc.Director._getInstance(...).getTouchDispatcher is not a function,百度后知道3.0后用了新的机制,但是代码都没找到javascript,后来在cocoachina中找到了例子,随手记录一下

原来注册事件代码:

cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 0, true);//把当前对象加入到触摸监听行列

3.0代码:

var touchListener = cc.EventListener.create({
    event: cc.EventListener.TOUCH_ONE_BY_ONE,//单点触摸
    swallowTouches: true,
    onTouchBegan:function (touches, event) {},
    onTouchMoved:function (touches, event) {},
    onTouchEnded:function (touches, event) {}
  });
cc.eventManager.addListener(touchListener, this);

event有几个选项,不同选项后面方法参数名称会有不一样,如:

cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ALL_AT_ONCE,//多点触摸
            onTouchesEnded: function (touches, event) {
                var touch = touches[0];
                var pos = touch.getLocation();
            },
            onTouchesBegan: function (touches, event) {},
            onTouchesMoved: function (touches, event) {},
            onTouchesEnded: function (touches, event) {},
            onTouchesCancelled: function (touches, event) {},
        }, this);  
原文地址:https://www.cnblogs.com/youbii/p/4046022.html