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.scale = 1 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() { this.node.scale = 0.9 if(this.startCallback){ this.startCallback(); } } private _touchShootEndEvent() { this.node.scale = 1 if(this.endCallback){ this.endCallback(); } } }