BtnExtend.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. console.log('zh:btnExtend.ts onLoad');
  25. // console.log('zh:btnExtend.ts EventType=',EventType)
  26. this.btn = this.getComponent(Button)
  27. //console.log('zh:btnExtend.ts btn Button=',this.btn)
  28. this.btn.zoomScale = this.zoomScale
  29. this.btn.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
  30. this.btn.node.on(EventType.Click, this.onClick, this)
  31. }
  32. protected onTouchStart(): void {
  33. this.handleClickSfx()
  34. }
  35. protected onClick(): void {
  36. console.log('zh:btnExtend.ts onClick');
  37. this.handleSafe()
  38. if (this.openUI) UIMgr.open(this.openUI)
  39. if (this.closeUI) UIMgr.close(this.closeUI)
  40. }
  41. private handleSafe(): void {
  42. if (!this.isSafe) return
  43. this.isSafe = false
  44. this.clickEvents = this.btn.clickEvents
  45. console.log('zh:btnExtend.ts handleSafe')
  46. this.btn.clickEvents = []
  47. this.scheduleOnce(() => {
  48. this.isSafe = true
  49. this.btn.clickEvents = this.clickEvents
  50. }, this.safeTime)
  51. }
  52. private handleClickSfx(): void {
  53. console.log('zh:btnExtend.ts handleClickSfx');
  54. this.clickSfx && AudioMgr.playSfx(this.clickSfx)
  55. }
  56. }