zoukankan      html  css  js  c++  java
  • 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);  
  • 相关阅读:
    常用的20个正则表达式
    关于position:fixed;的居中问题
    Html table 合并单元格
    JS异步加载的三种方式
    DOM事件代码小结
    用js写一个回车键盘事件
    JavaScript 常用方法总结
    可拖拽进度条
    js数组拍平
    js验证码倒计时
  • 原文地址:https://www.cnblogs.com/youbii/p/4046022.html
Copyright © 2011-2022 走看看