cocos2d-html5学习之三-为sprite添加触摸事件

在斗地主中,使用了cc.Sprite来实现扑克,但是cc.Sprite默认并不能接收触摸事件,需要手动将它注册到事件分配器中。

 

1. 在onEnter中注册为代理,由于扑克牌会产生重叠,在选择的时候不能让触摸事件传递到被覆盖的牌上,因此不能使用standardTargetedDelegate。

onEnter:function(){
        cc.registerTargetedDelegate(0, true, this);
        this._touchEnabled=true;
        this._super();
    }

2. 实现其它几个触摸事件,其中onTouchBegan中需要返回true,否则不会调用后面的onTouchEnded方法。

 

onTouchBegan:function(touches,event){
    	var rect = this.touchRect();
    	var point = touches.getLocation();
        if(cc.rectContainsPoint(this.touchRect(),touches.getLocation())){
            this._touchBegan=true;
            return true;
        }

        return false;
    }

 

    onTouchEnded:function(touches,event){
        if(this._touchBegan){
            this._touchBegan=false;

            if(this.active) {
        		this.active = false;

        		this.setPositionY(this.getPositionY() - 30);
        	}
        	else {
        		this.active = true;

        		this.setPositionY(this.getPositionY() + 30);
	        }
        }
    }

 

详情见:戴维营教育

转载于:https://www.cnblogs.com/jsxh/p/3495158.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值