EventButton.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.scale = 1
  27. this.node.on(cc.Node.EventType.TOUCH_START, this._touchShootStartEvent, this);
  28. this.node.on(cc.Node.EventType.TOUCH_END, this._touchShootEndEvent, this);
  29. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchShootEndEvent, this);
  30. }
  31. /**
  32. * 取消注册
  33. */
  34. public offTouchEvent(){
  35. this.startCallback = null;
  36. this.endCallback = null;
  37. this.node.off(cc.Node.EventType.TOUCH_START)
  38. this.node.off(cc.Node.EventType.TOUCH_END)
  39. this.node.off(cc.Node.EventType.TOUCH_CANCEL)
  40. }
  41. private _touchShootStartEvent() {
  42. this.node.scale = 0.9
  43. if(this.startCallback){
  44. this.startCallback();
  45. }
  46. }
  47. private _touchShootEndEvent() {
  48. this.node.scale = 1
  49. if(this.endCallback){
  50. this.endCallback();
  51. }
  52. }
  53. }