BtnClosePanel.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * 关闭指定面板
  4. * @author 薛鸿潇
  5. */
  6. @ccclass
  7. export default class BtnClosePanel extends cc.Component {
  8. @property({ displayName: '关闭界面', tooltip: '', type: cc.Node })
  9. private node_panel: cc.Node = null!;;
  10. @property({ displayName: '点击效果', tooltip: "仅支持 NONE 和 SCALE", type: cc.Enum(cc.Button.Transition) })
  11. public transition_type: cc.Button.Transition = cc.Button.Transition.NONE;
  12. @property({ displayName: '完成回调', tooltip: "面板打开完成后触发", type: cc.Component.EventHandler })
  13. public onComplete: cc.Component.EventHandler[] = [];
  14. /** 阻断点击 */
  15. public block_click: boolean = false;
  16. onLoad() {
  17. if (!this.node.getComponent(cc.Button)) {
  18. let btn_comp = this.node.addComponent(cc.Button);
  19. btn_comp.transition = this.transition_type;
  20. }
  21. }
  22. start() {
  23. this.node.on('click', this.closePanel, this);
  24. }
  25. private async closePanel() {
  26. mk.audio.playEffect("button");
  27. if (this.block_click) return;
  28. if (this.node_panel) {
  29. mk.ui.closePanel(this.node_panel.name);
  30. }
  31. const c_count = this.onComplete.length;
  32. for (let i = 0; i < c_count; i++) {
  33. if (this.onComplete[i].handler) this.onComplete[i].emit([])
  34. }
  35. }
  36. // update (dt) {}
  37. }