cocos2d-x触摸事件处理的javascript开发出现getTouchDispatcher is not a function
来源:互联网 发布:手机淘宝怎么扫码 编辑:程序博客网 时间:2024/04/29 21:25
转载出处: cocos2d-x的javascript开发出现 TypeError: ... getTouchDispatcher is not a function
使用国内开源跨平台游戏引擎cocos2d-x 2.1.4做游戏开发的时候,需要作一个方向盘,触摸某个方向,player就往哪个方向移动,自己采用sprite加载一张图片做为这个方向盘,在做sprite的触摸事件的时候报错:
TypeError: cc.Director.getInstance(...).getTouchDispatcher is not a function
关键代码是模仿cocos2d-x sdk中的demo来写的,网上搜了搜,方向这样的问题并不多,最后在cocos2d-x官网论坛上找到答案(http://www.cocos2d-x.org/boards/20/topics/24687?r=28076)
最后将自己写的一个具有触摸事件sprite贴出来供大家参考。
/** * Created with JetBrains WebStorm. * User: admin * Date: 13-8-19 * Time: 下午9:55 * To change this template use File | Settings | File Templates. *//** * 控制tank移动的方向盘 * @type {*} */var TouchControl = cc.Sprite.extend({ master:null, _rect:null, ctor:function(master) { this._super(); this.master = master; this.initWithTexture(cc.TextureCache.getInstance().addImage(res_touch_control_img)); }, initWithTexture:function (aTexture) { this._super(aTexture) if (aTexture instanceof cc.Texture2D) { var s = aTexture.getContentSize(); this._rect = cc.rect(0, 0, s.width, s.height); } else if ((aTexture instanceof HTMLImageElement) || (aTexture instanceof HTMLCanvasElement)) { this._rect = cc.rect(0, 0, aTexture.width, aTexture.height); } return true; }, onTouchEnded:function (touch, event) { cc.log('touch end'); }, onTouchMoved:function (touch, event) { cc.log('touch move'); }, onTouchBegan:function (touch, event) { var pos = touch.getLocation(); if (!this.containsTouchLocation(touch)) { return false; } return true; }, onTouchCancelled:function(touch, event) { cc.log('touch cancel'); }, touchDelegateRetain:function () { }, touchDelegateRelease:function () { }, onEnter:function () { //cc.Director.getInstance().getTouchDispatcher().addStandardDelegate(this, 0); cc.registerTargettedDelegate(0, true, this); // 使用这个代替 this._super(); }, onExit:function () { //cc.Director.getInstance().getTouchDispatcher().removeDelegate(this); cc.unregisterTouchDelegate(this); // 使用这个代替 this._super(); }, containsTouchLocation:function (touch) { cc.log('TouchControl - containsTouchLocation'); var getPoint = touch.getLocation(); var myRect = cc.rect(-this._rect.width / 2, -this._rect.height / 2, this._rect.width, this._rect.height); myRect.x += this.getPosition().x; myRect.y += this.getPosition().y; return cc.rectContainsPoint(myRect, getPoint);//this.convertTouchToNodeSpaceAR(touch)); }});
onTouchBegan方法是重写sprite中的方法,需要范围true, false,用来告知是否是一个合法的触摸操作。
onTouchEnded方法处理触摸之后进行的操作。
onTouchMoved方法处理触摸移动的操作
onTouchCancelled方法处理触摸取消的操作,一般将资源,显示等归位。
onEnter和onExit方法中是注册触摸监听器,注释掉的是之前我写的,不可用,换成下面的就可以了。
Enjoy~
0 0
- cocos2d-x触摸事件处理的javascript开发出现getTouchDispatcher is not a function
- Cocos2d-x游戏开发之处理单点触摸事件
- Cocos2d-x事件交互处理(触摸处理)
- javascript *** is not a function
- cocos2d-x 触摸事件处理机制
- Cocos2d-x 3.0 触摸事件处理机制
- Cocos2d-x 3.0 触摸事件处理机制
- cocos2d-x (四):触摸事件处理机制
- Cocos2d-x开发实例:单点触摸事件
- 【cocos2d-x游戏开发】触摸事件
- 出现$(#form).validate is not a function的问题
- 出现$(#form).validate is not a function的问题
- 出现$(#form).validate is not a function的问题
- [ IOS-Cocos2d-x 游戏开发之一] -cocos2d-x触摸事件优先级的探究与实践
- Javascript Error: submit is not a function
- str.lastIndexOf "is not a function" javascript
- cocos2d-x的CCLayer触摸事件
- cocos2d-x的两种触摸事件
- android开发:android获取IMEI、MSISDN、ICCID、IMSI
- 聚类算法学习笔记(1)
- 最好玩的休闲捕鱼游戏:“全球捕鱼”
- TextView功能总结(4)
- 多视图传参数(简单传值,全局变量)
- cocos2d-x触摸事件处理的javascript开发出现getTouchDispatcher is not a function
- 机房收费系统之组合查询
- Android--JSON使用
- 自定义 Preference
- 云上的云:AVOS Cloud在云平台上构建云服务的经验分享
- linux c++下信号处理
- 黑马程序员-1.5新特性 自动拆箱装箱
- orale 当这个字段为空时更新为同一个表的另一个字段的内容
- Java 网络编程初步