
在斗地主中,使用了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);
}
}
}
詳情見:戴維營教育
