const {ccclass, property} = cc._decorator; /** * 主界面的事件按钮 */ @ccclass export default class EventButton extends cc.Component { @property(cc.Sprite) mIcon: cc.Sprite = null; private callback:()=>void; private startCallback:()=>void; private endCallback:()=>void; public setCallback(callback:()=>void){ this.callback = callback; } public onclick(){ if(this.callback){ this.callback(); } } /** * 注册按压事件 */ public onTouchEvent(startCallback:()=>void,endCallback:()=>void){ this.startCallback = startCallback; this.endCallback = endCallback; this.node.on(cc.Node.EventType.TOUCH_START, this._touchShootStartEvent, this); this.node.on(cc.Node.EventType.TOUCH_END, this._touchShootEndEvent, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchShootEndEvent, this); } /** * 取消注册 */ public offTouchEvent(){ this.startCallback = null; this.endCallback = null; this.node.off(cc.Node.EventType.TOUCH_START) this.node.off(cc.Node.EventType.TOUCH_END) this.node.off(cc.Node.EventType.TOUCH_CANCEL) } private _touchShootStartEvent() { if(this.startCallback){ this.startCallback(); } } private _touchShootEndEvent() { if(this.endCallback){ this.endCallback(); } } }