touch.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { EventMgr } from "../../Script/Core/Base/EventMgr";
  2. import { Touch_Start, Touch_Move, Touch_End } from "./eventDef";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class touch extends cc.Component {
  6. /*===========================自动绑定组件开始===========================*/
  7. /*===========================自动绑定组件结束===========================*/
  8. /*===========================自动生成按钮事件开始==========================*/
  9. /*===========================自动生成按钮事件结束==========================*/
  10. touchId: number = -1;
  11. protected onEnable(): void {
  12. this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  13. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  14. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  15. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  16. }
  17. protected onDisable(): void {
  18. this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  19. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  20. this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  21. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  22. }
  23. onTouchStart(event: cc.Event.EventTouch) {
  24. if(this.touchId != -1) return;
  25. this.touchId = event.getID();
  26. EventMgr.inst.emit(Touch_Start);
  27. }
  28. onTouchMove(event: cc.Event.EventTouch) {
  29. if(this.touchId != event.getID()) return;
  30. let offset = event.getLocation().sub(event.getStartLocation());
  31. EventMgr.inst.emit(Touch_Move, offset);
  32. }
  33. onTouchEnd(event: cc.Event.EventTouch) {
  34. if(this.touchId != event.getID()) return;
  35. this.touchId = -1;
  36. EventMgr.inst.emit(Touch_End);
  37. }
  38. }