在鬥地主中,使用了cc.Sprite來實現撲克,可是cc.Sprite默認並不能接收觸摸事件,須要手動將它註冊到事件分配器中。
1. 在onEnter中註冊爲代理,因爲撲克牌會產生重疊,在選擇的時候不能讓觸摸事件傳遞到被覆蓋的牌上,所以不能使用standardTargetedDelegate。javascript
onEnter:function(){ cc.registerTargetedDelegate(0, true, this); this._touchEnabled=true; this._super(); }
2. 實現其它幾個觸摸事件,其中onTouchBegan中須要返回true,不然不會調用後面的onTouchEnded方法。java
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); } } }
詳情見:戴維營教育this