COCOS CREATOR(TS)之節點鼠標事件

一 : 前景

以官方的Demo爲例子(HelloWorld)
①-> UI層級結構
COCOS CREATOR(TS)之節點鼠標事件
②-> Canvas的屬性
COCOS CREATOR(TS)之節點鼠標事件

二 : 編碼(Helloworld.ts)

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    start () {
        // init logic
        this.label.string = this.text;
        this.listener2Handler(true);
    }

    private listener2Handler( $isAdd : boolean ) : void{
        if( $isAdd ){
            !this.node.hasEventListener(cc.Node.EventType.TOUCH_END) && this.node.on( cc.Node.EventType.TOUCH_END , this.onClick , this );
        }else{
            this.node.hasEventListener(cc.Node.EventType.TOUCH_END) && this.node.off( cc.Node.EventType.TOUCH_END , this.onClick , this );
        }
    }

    private onClick( $e : cc.Event.EventTouch ) : void{
        switch ($e.currentTarget) {
            case this.node:
                console.log("okok");
                break;
        }
    }

    onDestroy() : void{
        this.listener2Handler(false);
    }
}

三 : 結果

COCOS CREATOR(TS)之節點鼠標事件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章