Obstacle.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var cfg = require("Configure")
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. scoreSp: {
  6. default: null,
  7. type: cc.Sprite
  8. },
  9. budingTypeSp: {
  10. default: null,
  11. type: cc.Sprite
  12. },
  13. obstacleType: 0,
  14. colorType: 0,
  15. obstacleNum: 0,
  16. curColorType: 0,
  17. updateTime: 0,
  18. },
  19. // LIFE-CYCLE CALLBACKS:
  20. // onLoad () {},
  21. //初始化障碍物
  22. //type:障碍物类型(随机)
  23. initObstacle: function (type, obstacleNum) {
  24. this.obstacleNum = obstacleNum;
  25. if (obstacleNum == 1) {
  26. this.budingTypeSp.node.active = false;
  27. }
  28. this.obstacleType = type;
  29. switch (type) {
  30. case cfg.obstacleType.circle0:
  31. case cfg.obstacleType.square:
  32. this.initCircle0(type);
  33. break;
  34. case cfg.obstacleType.circle1:
  35. case cfg.obstacleType.circle2:
  36. case cfg.obstacleType.circle3:
  37. this.initCircle1(type);
  38. break;
  39. case cfg.obstacleType.translation:
  40. this.initTranslation(type);
  41. break;
  42. default:
  43. break;
  44. }
  45. },
  46. initCircle0: function (type) {
  47. var d = this.randDir();
  48. var t = this.randType();
  49. var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
  50. var config = {
  51. dir: d,
  52. type: t,
  53. angle: cfg.obstacleProperty[type].rotAngle,
  54. time: dt
  55. }
  56. var circleMode = this.node.getChildByName("Circle").getComponent("CircleMode");
  57. circleMode.initProperties(config);
  58. },
  59. initCircle1: function (type) {
  60. var t = this.randType();
  61. var d = this.randDir();
  62. var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
  63. var config1 = {
  64. dir: d,
  65. type: t,
  66. angle: cfg.obstacleProperty[type].rotAngle,
  67. time: dt,
  68. }
  69. var config2 = {
  70. dir: -d,
  71. type: t,
  72. angle: cfg.obstacleProperty[type].rotAngle,
  73. time: dt,
  74. }
  75. var circleMode1 = this.node.getChildByName("Circle1").getComponent("CircleMode");
  76. circleMode1.initProperties(config1);
  77. var circleMode2 = this.node.getChildByName("Circle2").getComponent("CircleMode");
  78. circleMode2.initProperties(type == cfg.obstacleType.circle1 ? config1 : config2);
  79. },
  80. initTranslation: function (type) {
  81. var d = this.randDir();
  82. var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].moveTime : cfg.obstacleProperty[type].moveTime;
  83. var config1 = {
  84. dir: d,
  85. distance: cfg.obstacleProperty[type].moveDistance,
  86. time: dt,
  87. }
  88. var config2 = {
  89. dir: -d,
  90. distance: cfg.obstacleProperty[type].moveDistance,
  91. time: dt,
  92. }
  93. for (let i = 0; i < 4; i++) {
  94. var transMode = this.node.getChildByName("Translation" + i).getComponent("TranslationMode");
  95. transMode.initProperties((i == 0 || i == 1) ? config1 : config2);
  96. }
  97. },
  98. randDir: function () {
  99. return Math.round(Math.random()) == 0 ? 1 : -1;
  100. },
  101. randType: function () {
  102. if(this.obstacleNum >= 10) {
  103. return Math.round(Math.random());
  104. } else {
  105. return 0;
  106. }
  107. },
  108. pauseObstacleAction: function () {
  109. switch (this.obstacleType) {
  110. case cfg.obstacleType.circle0:
  111. case cfg.obstacleType.square:
  112. this.node.getChildByName("Circle").getComponent("CircleMode").pauseAction();
  113. break;
  114. case cfg.obstacleType.circle1:
  115. case cfg.obstacleType.circle2:
  116. case cfg.obstacleType.circle3:
  117. this.node.getChildByName("Circle1").getComponent("CircleMode").pauseAction();
  118. this.node.getChildByName("Circle2").getComponent("CircleMode").pauseAction();
  119. break;
  120. case cfg.obstacleType.translation:
  121. this.node.getChildByName("Translation0").getComponent("TranslationMode").pauseAction();
  122. this.node.getChildByName("Translation1").getComponent("TranslationMode").pauseAction();
  123. this.node.getChildByName("Translation2").getComponent("TranslationMode").pauseAction();
  124. this.node.getChildByName("Translation3").getComponent("TranslationMode").pauseAction();
  125. break;
  126. default:
  127. break;
  128. }
  129. },
  130. resumeObstacleAction: function () {
  131. switch (this.obstacleType) {
  132. case cfg.obstacleType.circle0:
  133. case cfg.obstacleType.square:
  134. this.node.getChildByName("Circle").getComponent("CircleMode").resumeAction();
  135. break;
  136. case cfg.obstacleType.circle1:
  137. case cfg.obstacleType.circle2:
  138. case cfg.obstacleType.circle3:
  139. this.node.getChildByName("Circle1").getComponent("CircleMode").resumeAction();
  140. this.node.getChildByName("Circle2").getComponent("CircleMode").resumeAction();
  141. break;
  142. case cfg.obstacleType.translation:
  143. this.node.getChildByName("Translation0").getComponent("TranslationMode").resumeAction();
  144. this.node.getChildByName("Translation1").getComponent("TranslationMode").resumeAction();
  145. this.node.getChildByName("Translation2").getComponent("TranslationMode").resumeAction();
  146. this.node.getChildByName("Translation3").getComponent("TranslationMode").resumeAction();
  147. break;
  148. default:
  149. break;
  150. }
  151. },
  152. getType: function () {
  153. return this.obstacleType;
  154. },
  155. getColorType: function () {
  156. return this.curColorType;
  157. },
  158. moveObstacle: function (movePosY) {
  159. this.node.runAction(cc.moveBy(0.5, cc.p(0, movePosY)));
  160. },
  161. outScreen: function () {
  162. switch (this.obstacleType) {
  163. case cfg.obstacleType.circle0:
  164. case cfg.obstacleType.circle1:
  165. case cfg.obstacleType.translation:
  166. return this.node.y < -1000;
  167. break;
  168. case cfg.obstacleType.circle2:
  169. return this.node.y < -1100;
  170. break;
  171. case cfg.obstacleType.circle3:
  172. return this.node.y < -1720;
  173. break;
  174. case cfg.obstacleType.square:
  175. return this.node.y < -920;
  176. break;
  177. default:
  178. return false;
  179. break;
  180. }
  181. },
  182. start() {
  183. },
  184. update(dt) {
  185. this.updateTime += dt;
  186. //每隔1秒变1次色
  187. if (this.updateTime >= 1) {
  188. //变色块变色
  189. this.colorType++;
  190. if (this.colorType > 3) {
  191. this.colorType = 0;
  192. }
  193. if (this.obstacleType == cfg.obstacleType.circle3 || this.obstacleType == cfg.obstacleType.circle2) {
  194. if (this.colorType == 1 || this.colorType == 3) {
  195. this.colorType = Math.floor(Math.random() * 2) == 0 ? 0 : 2;
  196. }
  197. }
  198. var colorType = this.colorType;
  199. var self = this;
  200. cc.loader.loadRes(cfg.ColorTypePath[colorType], cc.SpriteFrame, function (err, spriteFrame) {
  201. self.budingTypeSp.spriteFrame = spriteFrame;
  202. self.curColorType = colorType;
  203. })
  204. this.updateTime = 0;
  205. }
  206. // if (this.node.active && this.outScreen())
  207. // {
  208. // this.node.active = false;
  209. // this.node.parent.getComponent("GameSence").destroyObstacle();
  210. // }
  211. }
  212. });