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);
	        }
        }
    }

 

詳情見:戴維營教育


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM