BaseButton.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { _decorator, EventTouch, Node, Enum, Event, CCInteger, Button, color, EventHandler, Component } from 'cc';
  2. import { aa } from '../aa';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('BaseButton')
  5. export class BaseButton extends Component {
  6. private isOK = true
  7. protected onEnable(): void {
  8. this.init()
  9. }
  10. list: ButtonCallbackInfo[] = [];
  11. init() {
  12. let btn = this.getComponent(Button) || this.addComponent(Button);
  13. if (!btn) return;
  14. btn.transition = 1;
  15. // btn.normalColor = color(255, 255, 255, 255);
  16. btn.pressedColor = color(221, 221, 221, 255);
  17. btn.hoverColor = color(211, 211, 211, 255);
  18. btn.disabledColor = color(124, 124, 124, 255);
  19. }
  20. // 不支持关闭对应方法
  21. on(type, callback: Function, target?, daly = 0.5, useCapture?) {
  22. // 这里要用动态代理
  23. let _callback = (ev) => {
  24. // aa.audio.getInstance().playOneShot('audios/clickUI')
  25. if (this.isOK) {
  26. this.isOK = false;
  27. this.scheduleOnce( ()=> {
  28. this.isOK = true;
  29. }, daly);
  30. callback.call(target,ev)
  31. }
  32. }
  33. this.list.push(new ButtonCallbackInfo(callback, _callback))
  34. this.node.on(type, _callback, target, useCapture);
  35. }
  36. off(type, callback: Function, target?, useCapture?) {
  37. let _callback = undefined
  38. for (let i = this.list.length - 1; i >= 0; i--) {
  39. let buttonCallbackInfo = this.list[i]
  40. if (buttonCallbackInfo.keyInfo = callback) {
  41. _callback = buttonCallbackInfo.vlaueInfo;
  42. this.list.splice(i, 1);
  43. break;
  44. }
  45. }
  46. this.node.off(type, _callback, target, useCapture);
  47. }
  48. }
  49. class ButtonCallbackInfo {
  50. keyInfo: Function;
  51. vlaueInfo: Function
  52. constructor(key: Function, value: Function) {
  53. this.keyInfo = key
  54. this.vlaueInfo = value
  55. }
  56. }