/** @format */ import UserButtonData from './UserButtonData' import {ccUtils} from '../utils/ccUtils' import {Mgr} from '../GameControl' import {SOUND} from '../enums/Sound' const {ccclass, requireComponent, executeInEditMode, menu, inspector} = cc._decorator @ccclass @menu('自定义组件/UserButton') @executeInEditMode() @requireComponent(UserButtonData) @inspector('packages://inspector/inspectors/comps/button.js') export default class UserButton extends cc.Button { public buttonData: UserButtonData public openContinuous = true public continuousTime: number = 0.2 public continuous: boolean = false public _continuousTimer: any = -1 public multiTouch: boolean = true public onEnable() { this.buttonData = this.node.getComponent(UserButtonData) if (this.buttonData) { this.openContinuous = this.buttonData.openContinuous this.continuousTime = this.buttonData.continuousTime this.multiTouch = this.buttonData.multiTouch } this.continuous = false // this.transition = cc.Button.Transition.SCALE; super.onEnable() } public onDisable() { if (this._continuousTimer >= 0) { clearTimeout(this._continuousTimer) this._continuousTimer = -1 } super.onDisable() } // tslint:disable-next-line: function-name public _onTouchBegan(event) { if (!this.interactable || !this.enabledInHierarchy) { return } if (!this.multiTouch && ccUtils.cannotTouch()) { return } this['_pressed'] = true this['_updateState']() Mgr.audio.playSFX(SOUND.clickBtn) event.stopPropagation() } // tslint:disable-next-line: function-name public _onTouchEnded(event) { if (!this.interactable || !this.enabledInHierarchy) { return } if (this['_pressed'] && !this.continuous) { this.continuous = !!this.openContinuous cc.Component.EventHandler.emitEvents(this.clickEvents, event) this.node.emit('click', this) if (this.openContinuous) { this._continuousTimer = setTimeout(() => { this.continuous = false }, this.continuousTime * 1000) } } this['_pressed'] = false this['_updateState']() event.stopPropagation() } }