DebugCtrl.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. cc.Class({
  2. extends: cc.Component,
  3. editor: {
  4. menu: 'Debug/DebugCtrl'
  5. },
  6. properties: {
  7. btnNode: cc.Node,
  8. _prevX: 0,
  9. _prevY: 0,
  10. _didMove: false
  11. },
  12. onEnable () {
  13. this.btnNode.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
  14. this.btnNode.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
  15. this.btnNode.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
  16. this.btnNode.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  17. cc.tween(this.btnNode)
  18. .delay(1)
  19. .to(1, {opacity: 0})
  20. .start();
  21. },
  22. onDisable () {
  23. this.btnNode.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
  24. this.btnNode.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
  25. this.btnNode.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
  26. this.btnNode.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  27. },
  28. onClicked () {
  29. G.AppUtils.getSceneCtrl().showAlert('edt_prefab/Debug/debug_alert', undefined);
  30. },
  31. _onTouchStart (event) {
  32. let location = event.getLocation();
  33. this._didMove = false;
  34. this.btnNode.stopAllActions();
  35. this.btnNode.opacity = 255;
  36. this._prevX = location.x;
  37. this._prevY = location.y;
  38. },
  39. _onTouchMove (event) {
  40. let location = event.getLocation();
  41. let posX = this.btnNode.x;
  42. let posY = this.btnNode.y;
  43. if (!this._didMove) {
  44. let deltaV2 = cc.v2(posX, posY).sub(cc.v2(location.x, location.y));
  45. let dist = deltaV2.mag();
  46. if (dist > 44) {
  47. this._didMove = true;
  48. }
  49. }
  50. if (this._didMove) {
  51. this.btnNode.x = posX + (location.x - this._prevX);
  52. this.btnNode.y = posY + (location.y - this._prevY);
  53. this._prevX = location.x;
  54. this._prevY = location.y;
  55. }
  56. },
  57. _onTouchEnd (event) {
  58. if (!this._didMove) {
  59. this.onClicked();
  60. }
  61. this._didMove = false;
  62. this._prevX = 0;
  63. this._prevY = 0;
  64. cc.tween(this.btnNode)
  65. .delay(1)
  66. .to(1, {opacity: 0})
  67. .start();
  68. }
  69. });