| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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() {
- this.node_normal.active = false;
- this.node_pressed.active = true;
- this.node_disabled.active = false;
- }
- private touchEnd() {
- this.node_normal.active = true;
- this.node_pressed.active = false;
- this.node_disabled.active = false;
- }
- private click() {
- 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 = [];
- }
- }
|