1. 程式人生 > >COCOS CREATOR(TS)之節點鼠標事件

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

false case efault seve label img 分享 decorator 結果

一 : 前景

以官方的Demo為例子(HelloWorld)
①-> UI層級結構
技術分享圖片
②-> Canvas的屬性
技術分享圖片

二 : 編碼(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)之節點鼠標事件