EventButton.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const {ccclass, property} = cc._decorator;
  2. /**
  3. * 主界面的事件按钮
  4. */
  5. @ccclass
  6. export default class EventButton extends cc.Component {
  7. @property(cc.Sprite)
  8. mIcon: cc.Sprite = null;
  9. private callback:()=>void;
  10. private startCallback:()=>void;
  11. private endCallback:()=>void;
  12. public setCallback(callback:()=>void){
  13. this.callback = callback;
  14. }
  15. public onclick(){
  16. if(this.callback){
  17. this.callback();
  18. }
  19. }
  20. /**
  21. * 注册按压事件
  22. */
  23. public onTouchEvent(startCallback:()=>void,endCallback:()=>void){
  24. this.startCallback = startCallback;
  25. this.endCallback = endCallback;
  26. this.node.on(cc.Node.EventType.TOUCH_START, this._touchShootStartEvent, this);
  27. this.node.on(cc.Node.EventType.TOUCH_END, this._touchShootEndEvent, this);
  28. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchShootEndEvent, this);
  29. }
  30. /**
  31. * 取消注册
  32. */
  33. public offTouchEvent(){
  34. this.startCallback = null;
  35. this.endCallback = null;
  36. this.node.off(cc.Node.EventType.TOUCH_START)
  37. this.node.off(cc.Node.EventType.TOUCH_END)
  38. this.node.off(cc.Node.EventType.TOUCH_CANCEL)
  39. }
  40. private _touchShootStartEvent() {
  41. if(this.startCallback){
  42. this.startCallback();
  43. }
  44. }
  45. private _touchShootEndEvent() {
  46. if(this.endCallback){
  47. this.endCallback();
  48. }
  49. }
  50. }