Rocker.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { EventMgr } from "../Script/Core/Base/EventMgr";
  2. import { Roker_End, Roker_Move } from "./eventDef";
  3. const {ccclass, property} = cc._decorator;
  4. interface iRockerTemp {
  5. bgOriginPos: cc.Vec2;
  6. id: number;
  7. }
  8. @ccclass
  9. export default class Rocker extends cc.Component {
  10. /*===========================自动绑定组件开始===========================*/
  11. /*自动生成*/
  12. @property({type:cc.Node, displayName:""})
  13. private $bg_node:cc.Node = null;
  14. /*自动生成*/
  15. @property({type:cc.Node, displayName:""})
  16. private $point_node:cc.Node = null;
  17. /*自动生成*/
  18. @property({type:cc.Node, displayName:""})
  19. private $arrow_node:cc.Node = null;
  20. /*===========================自动绑定组件结束===========================*/
  21. /*===========================自动生成按钮事件开始==========================*/
  22. /*===========================自动生成按钮事件结束==========================*/
  23. temp: iRockerTemp = {
  24. bgOriginPos:null,
  25. id:-1,
  26. }
  27. protected onLoad(): void {
  28. this.temp.bgOriginPos = this.$bg_node.getPosition();
  29. }
  30. protected onEnable(): void {
  31. this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  32. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  33. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  34. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  35. }
  36. protected onDisable(): void {
  37. this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  38. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  39. this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  40. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  41. }
  42. onTouchStart(event: cc.Event.EventTouch) {
  43. if(this.temp.id != -1) return;
  44. let pos = event.getLocation();
  45. pos = this.node.convertToNodeSpaceAR(pos);
  46. this.$bg_node.setPosition(pos);
  47. this.temp.id = event.getID();
  48. }
  49. onTouchMove(event: cc.Event.EventTouch) {
  50. if(this.temp.id != event.getID()) return;
  51. let offset = event.getLocation().sub(event.getStartLocation());
  52. let angle = cc.v2(0,1).signAngle(offset) / Math.PI * 180;
  53. this.$arrow_node.angle = Math.round(angle * 100) / 100;
  54. let nOffset = offset.normalize();
  55. if(offset.mag() > 85) {
  56. this.$point_node.setPosition(nOffset.x * 85, nOffset.y * 85);
  57. }else{
  58. this.$point_node.setPosition(offset.x, offset.y);
  59. }
  60. EventMgr.inst.emit(Roker_Move, nOffset);
  61. }
  62. onTouchEnd(event: cc.Event.EventTouch) {
  63. if(this.temp.id != event.getID()) return;
  64. this.$bg_node.setPosition(this.temp.bgOriginPos);
  65. this.$arrow_node.angle = 0;
  66. this.$point_node.setPosition(0, 0);
  67. this.temp.id = -1;
  68. EventMgr.inst.emit(Roker_End);
  69. }
  70. }