cocos2d-x触摸事件处理的javascript开发出现getTouchDispatcher is not a function

转载出处: 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~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值