CSY.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. var Dir_Up = 1;
  2. var Dir_Down = 2;
  3. var Dir_Left = 4;
  4. var Dir_Right = 8;
  5. var Dir_Left_Up = 5;
  6. var Dir_Right_Up = 9;
  7. var Dir_Left_Down = 6;
  8. var Dir_Right_Down = 10;
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. m_TouchNode: cc.Node,
  13. m_ViewNode: cc.Node,
  14. m_TipSprite: cc.Sprite,
  15. },
  16. ctor: function () {
  17. this.m_Dir = 0; //方向
  18. this.m_TouchStartX = 0; //起始点
  19. this.m_TouchStartY = 0; //起始点
  20. this.m_TouchTurnX = 0; //拐点
  21. this.m_TouchTurnY = 0; //拐点
  22. this.m_ActMarkArr = new Array(4, 10, 5, 6); //← ↘ ↖ ↙ , 9 ↗
  23. this.m_MarkIndex = null;
  24. this.m_CurAct = null;
  25. this.m_ActTime = 0;
  26. this.m_ShowLogArr = new Array();
  27. this.m_ShowLogArr[1] = '↑'
  28. this.m_ShowLogArr[2] = '↓'
  29. this.m_ShowLogArr[4] = '←'
  30. this.m_ShowLogArr[8] = '→'
  31. this.m_ShowLogArr[5] = '↖'
  32. this.m_ShowLogArr[9] = '↗'
  33. this.m_ShowLogArr[6] = '↙'
  34. this.m_ShowLogArr[10] = '↘'
  35. },
  36. onLoad: function () {
  37. // this.m_TouchNode = this.$('Touch');
  38. // this.m_ViewNode = this.$('View');
  39. // this.m_TipSprite = this.$('Touch@Sprite');
  40. //按钮监听
  41. this.m_TouchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan.bind(this), this.m_TouchNode);
  42. this.m_TouchNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoved.bind(this), this.m_TouchNode);
  43. this.m_TouchNode.on(cc.Node.EventType.TOUCH_END, this.onTouchOver.bind(this), this.m_TouchNode);
  44. this.m_TouchNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchOver.bind(this), this.m_TouchNode);
  45. this.m_TipSprite.enabled = false;
  46. },
  47. SetHook: function (Hook) {
  48. this.m_Hook = Hook;
  49. },
  50. ShowView: function () {
  51. this.node.active = true;
  52. },
  53. HideView: function () {
  54. this.node.active = false;
  55. },
  56. //Func 回调参数 确定1 取消0 关闭null
  57. ShowAlert: function (str, style, Func, Hook) {
  58. cc.gPreLoader.LoadPrefab("Alert", function (Js) {
  59. this.node.addChild(Js.node);
  60. Js.ShowView();
  61. if (style == null) style = Alert_Yes; //默认参数
  62. Js.ShowAlert(str, style, Func, Hook);
  63. }.bind(this));
  64. },
  65. //触摸事件
  66. onTouchBegan: function (event) {
  67. event.stopPropagation();
  68. //滑动起始点
  69. this.m_TouchStartX = event.touch.getLocation().x;
  70. this.m_TouchStartY = event.touch.getLocation().y;
  71. this.m_TouchTurnX = event.touch.getLocation().x;
  72. this.m_TouchTurnY = event.touch.getLocation().y;
  73. this.m_TipSprite.enabled = true;
  74. this.m_MarkIndex = 0;
  75. this.m_ActTime = 0;
  76. this.m_CurAct = null;
  77. this.m_ViewNode.setPosition(-2000, 0);
  78. return true;
  79. },
  80. onTouchMoved: function (event) {
  81. if (this.m_MarkIndex == null) return
  82. //屏幕坐标转节点坐标
  83. var Now = new Date().getTime();
  84. var MoveX = this.m_TouchTurnX - event.touch.getLocation().x;
  85. var MoveY = this.m_TouchTurnY - event.touch.getLocation().y;
  86. //轨迹判断
  87. if (this.m_ActTime != 0 && Now - this.m_ActTime > 100) {
  88. this.m_TouchTurnX = event.touch.getLocation().x;
  89. this.m_TouchTurnY = event.touch.getLocation().y;
  90. var Mark = 0;
  91. if (Math.abs(MoveX) > 20) Mark += (MoveX > 0 ? Dir_Left : Dir_Right);
  92. if (Math.abs(MoveY) > 20) Mark += (MoveY > 0 ? Dir_Down : Dir_Up);
  93. if (this.m_CurAct == null) this.m_CurAct = Mark;
  94. //console.log('###',this.m_MarkIndex, this.m_ShowLogArr[Mark], Mark, Math.abs(MoveX),Math.abs(MoveY) )
  95. //拐点
  96. if (this.m_CurAct != Mark && this.m_ActMarkArr.length > this.m_MarkIndex) {
  97. if (this.m_CurAct != this.m_ActMarkArr[this.m_MarkIndex]) { //失败
  98. // console.log('onTouchMoved ',this.m_CurAct, this.m_ActMarkArr[this.m_MarkIndex], this.m_MarkIndex )
  99. this.m_MarkIndex = null;
  100. this.m_TipSprite.enabled = false;
  101. return
  102. } else {
  103. this.m_MarkIndex++;
  104. this.m_CurAct = Mark;
  105. }
  106. }
  107. }
  108. this.m_ActTime = Now;
  109. },
  110. onTouchOver: function (event) {
  111. if (this.m_MarkIndex == null) return
  112. var MoveX = this.m_TouchTurnX - event.touch.getLocation().x;
  113. var MoveY = this.m_TouchTurnY - event.touch.getLocation().y;
  114. var MoveX2 = this.m_TouchStartX - event.touch.getLocation().x;
  115. var MoveY2 = this.m_TouchStartY - event.touch.getLocation().y;
  116. var Mark = 0;
  117. if (Math.abs(MoveX) > 20) Mark += (MoveX > 0 ? Dir_Left : Dir_Right);
  118. if (Math.abs(MoveY) > 20) Mark += (MoveY > 0 ? Dir_Down : Dir_Up);
  119. if (this.m_CurAct == null) this.m_CurAct = Mark;
  120. //console.log('###',this.m_MarkIndex, this.m_ShowLogArr[Mark], Mark, Math.abs(MoveX),Math.abs(MoveY) )
  121. //console.log('###2 ',Math.abs(MoveX2) , Math.abs(MoveY2) )
  122. //拐点
  123. if (this.m_ActMarkArr.length > this.m_MarkIndex && Math.abs(MoveX2) < 50 && Math.abs(MoveY2) < 50) {
  124. if (this.m_CurAct == this.m_ActMarkArr[this.m_MarkIndex]) this.m_MarkIndex++;
  125. }
  126. if (this.m_ActMarkArr.length <= this.m_MarkIndex) {
  127. this.m_ViewNode.active = true;
  128. this.m_ViewNode.setPosition(0, 0);
  129. }
  130. this.m_MarkIndex = null;
  131. this.m_TipSprite.enabled = false;
  132. },
  133. OnClick_BtWaring: function (Data, bWaring) {
  134. var QueryW = new CMD_GP_C_Warning();
  135. QueryW.bWarning = parseInt(bWaring);
  136. var LoginMission = new CGPLoginMission(this, MDM_GP_MANAGER, SUB_GP_WARNING, QueryW);
  137. },
  138. OnClick_BtDestroy: function () {
  139. this.ShowAlert('確認?', Alert_All, function (Res) {
  140. if (Res) {
  141. var LoginMission = new CGPLoginMission(this, MDM_GP_MANAGER, SUB_GP_DESTROY, {}, 0);
  142. }
  143. }.bind(this));
  144. },
  145. OnMsgRes: function (Msg) {
  146. this.ShowAlert(Msg);
  147. },
  148. // update (dt) {},
  149. });