-
Creator 版本:v2.3.1
-
目标平台: web
写了如下一段代码:
const { ccclass, property } = cc._decorator;
@ccclass
export class NodeSwitch extends cc.Component {
@property() status: boolean = false;
@property(cc.Node) selected: cc.Node = null;
@property(cc.Node) unselected: cc.Node = null;
private emitStatusEvents(): void {
this.selected.active = this.status;
this.unselected.active = !this.status;
this.node.emit("node_switch", this);
}
private switchStatus() : void {
this.status = !this.status;
this.emitStatusEvents();
}
public setStatus(status : boolean) : void{
this.status = status;
this.emitStatusEvents();
}
onEnable(): void {
console.log("onEnable");
this.node.on("click", this.switchStatus, this);
}
onDisable(): void {
console.log("onDisable");
this.node.off("click", this.switchStatus, this);
}
}
想通过监听 click 事件,实现发送 node_switch 事件的目的。但是发现不会执行 click 监听的事件。
请教下代码有啥问题?
PS:参考的官方源码写法 cc.Toggle