Draw.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. index: 300,
  5. _graphics: cc.Graphics,
  6. //是否跟随目标来画路径
  7. followTarget: false,
  8. //目标位置
  9. target: cc.Node,
  10. _PStart: null,
  11. _pNext: null,
  12. _isInit: false,//只设置一次
  13. //是否使用数组来画路径
  14. followPath: false,
  15. //用点来初始化路径
  16. _initPoint: false,
  17. //开始第一次绘制
  18. _pointStart: false,
  19. },
  20. onLoad() {
  21. if (this.node.getComponent(cc.Graphics)) {
  22. this._graphics = this.node.getComponent(cc.Graphics);
  23. } else {
  24. this._graphics = this.node.addComponent(cc.Graphics);
  25. }
  26. },
  27. //根据目标画线
  28. DrawLineFromTarget(){
  29. if (this.followTarget && this.target) {
  30. this._graphics.clear();
  31. this._graphics.lineWidth = 3;
  32. this._graphics.strokeColor = cc.Color.GREEN;
  33. this._PStart = this.target.getPosition();
  34. this._POld = null;
  35. this._isSet = false;
  36. this._isInit = false;
  37. this.unschedule(this.callback);
  38. this.schedule(this.callback = function () {
  39. if (this._POld&&this._isSet) {
  40. if (this._POld.sub(this.target.position).mag() == 0) {
  41. // 取消这个计时器
  42. this.unschedule(this.callback);
  43. // cc.log('取消这个计时器.')
  44. }
  45. }
  46. if (this._isInit) {
  47. this._graphics.lineTo(this.target.x, this.target.y);
  48. }
  49. else {
  50. this._isInit = true;
  51. this._pNext = this.target.getPosition();
  52. this._graphics.moveTo(this._PStart.x, this._PStart.y);
  53. this._graphics.lineTo(this._pNext.x, this._pNext.y);
  54. }
  55. this._graphics.stroke();
  56. if(!this._isSet){
  57. this._POld = this.target.position;
  58. }
  59. this._isSet = !this._isSet;
  60. }, 0.1, 2000, 0);
  61. }
  62. },
  63. AddDrawNode() {
  64. this.index += 300;
  65. this._graphics.lineTo(this.index, this.index + Math.floor(Math.random() * 100));
  66. this._graphics.stroke();
  67. },
  68. InitDrawNode(pStart, pNext) {
  69. this._graphics.clear();
  70. this._graphics.lineWidth = 3;
  71. this._graphics.strokeColor = cc.Color.GREEN;
  72. this._graphics.moveTo(pStart.x, pStart.y);
  73. this._graphics.lineTo(pNext.x, pNext.y);
  74. this._graphics.stroke();
  75. },
  76. // UpdateTargetNode(target) {
  77. // this._graphics.lineTo(target.x, target.y);
  78. // this._graphics.stroke();
  79. // },
  80. //用路径来绘制
  81. //cc.v2
  82. onDrawFromPath(path) {
  83. if (!path) {
  84. cc.error('onDrawFromPath 参数为Null.');
  85. return;
  86. }
  87. var _initPath = false;
  88. this._graphics.clear();
  89. this._graphics.lineWidth = 3;
  90. this._graphics.strokeColor = cc.Color.YELLOW;
  91. let pathIndex = 0;
  92. let pStart = path[pathIndex];
  93. let pNext = null;
  94. this.schedule(function () {
  95. if (_initPath) {
  96. pathIndex++;
  97. if (pathIndex > path.length - 1) {
  98. cc.log('数组绘制完成。');
  99. return;
  100. }
  101. pNext = path[pathIndex];
  102. this._graphics.lineTo(pNext.x, pNext.y);
  103. this._graphics.stroke();
  104. }
  105. else {
  106. _initPath = true;
  107. pathIndex++;
  108. pNext = path[pathIndex];
  109. this._graphics.moveTo(pStart.x, pStart.y);
  110. this._graphics.lineTo(pNext.x, pNext.y);
  111. this._graphics.stroke();
  112. }
  113. }, 0.3, 2000, 1);
  114. },
  115. //用点来绘制
  116. //cc.v2
  117. onDrawFromPoint(point) {
  118. if (!point) {
  119. cc.error('onDrawFromPoint 参数为Null.');
  120. return;
  121. }
  122. if (!this._initPoint) {
  123. this._graphics.clear();
  124. this._graphics.lineWidth = 3;
  125. this._graphics.strokeColor = cc.Color.RED;
  126. this._PStart = point;
  127. this._initPoint = true;
  128. } else {
  129. this._pNext = point;
  130. if (!this._pointStart) {
  131. this._graphics.moveTo(this._PStart.x, this._PStart.y);
  132. this._graphics.lineTo(this._pNext.x, this._pNext.y);
  133. this._pointStart = true;
  134. } else {
  135. this._graphics.lineTo(this._pNext.x, this._pNext.y);
  136. }
  137. this._graphics.stroke();
  138. }
  139. },
  140. //cc.v2
  141. onDrawFromTwoPoints(point1, piont2) {
  142. // this._graphics.clear();
  143. this._graphics.strokeColor = cc.Color.GREEN;
  144. // this._graphics.strokeColor = cc.hexToColor('#FFFF00');
  145. this._graphics.moveTo(point1.x, point1.y);
  146. this._graphics.lineTo(piont2.x, piont2.y);
  147. },
  148. onStroke() {
  149. this._graphics.stroke();
  150. }
  151. // update(dt) {
  152. // },
  153. });