const { ccclass, property } = cc._decorator; @ccclass export default class Button extends cc.Component { @property({ type: cc.Node, displayName: "正常" }) node_normal: cc.Node = null; @property({ type: cc.Node, displayName: "按下" }) node_pressed: cc.Node = null; @property({ type: cc.Node, displayName: "禁用" }) node_disabled: cc.Node = null; @property({ displayName: "点击函数", type: cc.Component.EventHandler }) public onClick: cc.Component.EventHandler[] = []; onLoad() { this.touchEnd(); } start() { if (!this.node.getComponent(cc.Button)) { this.node.addComponent(cc.Button); } this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this); this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this); this.node.on("click", this.click, this); } private touchStart() { if(this.node_normal) this.node_normal.active = false; if(this.node_pressed) this.node_pressed.active = true; if(this.node_disabled) this.node_disabled.active = false; } private touchEnd() { if(this.node_normal) this.node_normal.active = true; if(this.node_pressed) this.node_pressed.active = false; if(this.node_disabled) this.node_disabled.active = false; } private click() { mk.audio.playEffect("button"); let c_count = this.onClick.length; for (let i = 0; i < c_count; i++) { if (this.onClick[i].handler) { this.onClick[i].emit([]); } } } onDestroy() { this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this); this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this); this.node.off("click", this.click, this); this.onClick = []; } }