COCOS CREATOR(TS)のノードマウスイベント
1214 ワード
一:前景
公式の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);
}
}
三:結果
転載先:https://blog.51cto.com/aonaufly/2352091
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);
}
}