BtnExtend.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { _decorator, Button, Component, EventHandler, Node } from 'cc';
  2. import { AudioMgr } from '../manager/AudioMgr';
  3. import { EventType } from '../enum/EventType';
  4. import { UIMgr } from '../manager/UIMgr';
  5. const { ccclass, property, disallowMultiple, requireComponent } = _decorator;
  6. @ccclass('UIExtend/BtnExtend')
  7. @disallowMultiple(true)
  8. @requireComponent(Button)
  9. export class BtnExtend extends Component {
  10. @property({ range: [0, 1, 0.1], slide: true })
  11. private safeTime: number = 0.5
  12. @property
  13. private zoomScale:number = 1.1
  14. @property
  15. private clickSfx: string = 'btnClick'
  16. @property
  17. private openUI: string = ''
  18. @property
  19. private closeUI: string = ''
  20. private btn: Button = null
  21. private clickEvents: EventHandler[]
  22. private isSafe: boolean = true
  23. protected onLoad() {
  24. this.btn = this.getComponent(Button)
  25. this.btn.zoomScale = this.zoomScale
  26. this.btn.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
  27. this.btn.node.on(EventType.Click, this.onClick, this)
  28. }
  29. protected onTouchStart(): void {
  30. this.handleClickSfx()
  31. }
  32. protected onClick(): void {
  33. this.handleSafe()
  34. if (this.openUI) UIMgr.open(this.openUI)
  35. if (this.closeUI) UIMgr.close(this.closeUI)
  36. }
  37. private handleSafe(): void {
  38. if (!this.isSafe) return
  39. this.isSafe = false
  40. this.clickEvents = this.btn.clickEvents
  41. this.btn.clickEvents = []
  42. this.scheduleOnce(() => {
  43. this.isSafe = true
  44. this.btn.clickEvents = this.clickEvents
  45. }, this.safeTime)
  46. }
  47. private handleClickSfx(): void {
  48. this.clickSfx && AudioMgr.playSfx(this.clickSfx)
  49. }
  50. }