Rocker.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { EventMgr } from "../../Script/Core/Base/EventMgr";
  2. import { Roker_Move, Roker_End } 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 = 0;
  53. if(!offset.equals(cc.Vec2.ZERO)) {
  54. angle = cc.v2(0,1).signAngle(offset) / Math.PI * 180;
  55. }
  56. this.$arrow_node.angle = Math.round(angle * 100) / 100;
  57. let nOffset = offset.normalize();
  58. if(offset.mag() > 85) {
  59. this.$point_node.setPosition(nOffset.x * 85, nOffset.y * 85);
  60. }else{
  61. this.$point_node.setPosition(offset.x, offset.y);
  62. }
  63. EventMgr.inst.emit(Roker_Move, nOffset);
  64. }
  65. onTouchEnd(event: cc.Event.EventTouch) {
  66. if(this.temp.id != event.getID()) return;
  67. this.$bg_node.setPosition(this.temp.bgOriginPos);
  68. this.$arrow_node.angle = 0;
  69. this.$point_node.setPosition(0, 0);
  70. this.temp.id = -1;
  71. EventMgr.inst.emit(Roker_End);
  72. }
  73. }