Obstacle.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. stopObstacleAction: function () {
  131. switch (this.obstacleType) {
  132. case cfg.obstacleType.circle0:
  133. case cfg.obstacleType.square:
  134. this.node.getChildByName("Circle").stopAllActions();
  135. break;
  136. case cfg.obstacleType.circle1:
  137. case cfg.obstacleType.circle2:
  138. case cfg.obstacleType.circle3:
  139. this.node.getChildByName("Circle1").stopAllActions();
  140. this.node.getChildByName("Circle2").stopAllActions();
  141. break;
  142. case cfg.obstacleType.translation:
  143. this.node.getChildByName("Translation0").stopAllActions();
  144. this.node.getChildByName("Translation1").stopAllActions();
  145. this.node.getChildByName("Translation2").stopAllActions();
  146. this.node.getChildByName("Translation3").stopAllActions();
  147. break;
  148. default:
  149. break;
  150. }
  151. },
  152. resumeObstacleAction: function () {
  153. switch (this.obstacleType) {
  154. case cfg.obstacleType.circle0:
  155. case cfg.obstacleType.square:
  156. this.node.getChildByName("Circle").getComponent("CircleMode").resumeAction();
  157. break;
  158. case cfg.obstacleType.circle1:
  159. case cfg.obstacleType.circle2:
  160. case cfg.obstacleType.circle3:
  161. this.node.getChildByName("Circle1").getComponent("CircleMode").resumeAction();
  162. this.node.getChildByName("Circle2").getComponent("CircleMode").resumeAction();
  163. break;
  164. case cfg.obstacleType.translation:
  165. this.node.getChildByName("Translation0").getComponent("TranslationMode").resumeAction();
  166. this.node.getChildByName("Translation1").getComponent("TranslationMode").resumeAction();
  167. this.node.getChildByName("Translation2").getComponent("TranslationMode").resumeAction();
  168. this.node.getChildByName("Translation3").getComponent("TranslationMode").resumeAction();
  169. break;
  170. default:
  171. break;
  172. }
  173. },
  174. getType: function () {
  175. return this.obstacleType;
  176. },
  177. getColorType: function () {
  178. return this.curColorType;
  179. },
  180. moveObstacle: function (movePosY) {
  181. this.node.runAction(cc.moveBy(0.5, cc.p(0, movePosY)));
  182. },
  183. outScreen: function () {
  184. switch (this.obstacleType) {
  185. case cfg.obstacleType.circle0:
  186. case cfg.obstacleType.circle1:
  187. case cfg.obstacleType.translation:
  188. return this.node.y < -1000;
  189. break;
  190. case cfg.obstacleType.circle2:
  191. return this.node.y < -1100;
  192. break;
  193. case cfg.obstacleType.circle3:
  194. return this.node.y < -1720;
  195. break;
  196. case cfg.obstacleType.square:
  197. return this.node.y < -920;
  198. break;
  199. default:
  200. return false;
  201. break;
  202. }
  203. },
  204. start() {
  205. },
  206. update(dt) {
  207. this.updateTime += dt;
  208. //每隔1秒变1次色
  209. if (this.updateTime >= 1) {
  210. //变色块变色
  211. this.colorType++;
  212. if (this.colorType > 3) {
  213. this.colorType = 0;
  214. }
  215. if (this.obstacleType == cfg.obstacleType.circle3 || this.obstacleType == cfg.obstacleType.circle2) {
  216. if (this.colorType == 1 || this.colorType == 3) {
  217. this.colorType = Math.floor(Math.random() * 2) == 0 ? 0 : 2;
  218. }
  219. }
  220. var colorType = this.colorType;
  221. var self = this;
  222. cc.loader.loadRes(cfg.ColorTypePath[colorType], cc.SpriteFrame, function (err, spriteFrame) {
  223. self.budingTypeSp.spriteFrame = spriteFrame;
  224. self.curColorType = colorType;
  225. })
  226. this.updateTime = 0;
  227. }
  228. // if (this.node.active && this.outScreen())
  229. // {
  230. // this.node.active = false;
  231. // this.node.parent.getComponent("GameSence").destroyObstacle();
  232. // }
  233. }
  234. });