cocos creator 如何用程式碼給按鈕元件新增自定義事件
阿新 • • 發佈:2021-01-10
技術標籤:cocos creator疑難雜症cocoscocos creator按鈕元件
//獲取按鈕元件
let button=this.node.getComponent(cc.Button);
//建立觸發事件
let targetEvent=new cc.Component.EventHandler();
targetEvent.target=this.node; //新增觸發模板節點
targetEvent.component='ButtonTest'; //目標節點的元件
targetEvent. handler='buttonClick' //目標元件中的函式
targetEvent.customEventData='動態事件';
//給該元件目標點選事件
button.clickEvents.push(targetEvent);
}
// update (dt) {}
buttonClick(e:cc.Event.EventTouch,msg:string){
cc.log('按鈕點選了:'+msg);
}