BuDingSprite.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var cfg = require("Configure")
  2. //布丁状态
  3. var State = cc.Enum({
  4. None: 0,
  5. Jump: 1,
  6. Drop: 2,
  7. Dead: 3
  8. })
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. gravity: 0,
  13. initJumpSpeed: 0,
  14. jumpAudio: {
  15. default: null,
  16. type: cc.AudioClip
  17. },
  18. deadAudio: {
  19. default: null,
  20. type: cc.AudioClip
  21. },
  22. shield: {
  23. default: null,
  24. type: cc.Node
  25. },
  26. shieldInvincible: false,
  27. skSprite: {
  28. default: null,
  29. type: cc.Node
  30. },
  31. //布丁状态
  32. _state: {
  33. default: State.None,
  34. type: State,
  35. visible: false
  36. },
  37. state: {
  38. get: function () {
  39. return this._state;
  40. },
  41. set: function (value) {
  42. if (value !== this._state) {
  43. this._state = value;
  44. //var animName = State[this._state];
  45. //this.anim.stop();
  46. //this.anim.play(animName);
  47. if (value != State.Dead) {
  48. this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], value == State.None);
  49. } else {
  50. cc.audioEngine.playEffect(this.deadAudio);
  51. this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], false);
  52. }
  53. }
  54. },
  55. type: State,
  56. },
  57. colorType: 0,
  58. groundY: 0,
  59. startGroundY: 0
  60. },
  61. // LIFE-CYCLE CALLBACKS:
  62. onLoad: function () {
  63. this.groundY = (-cc.winSize.height / 2 - 25) * (cc.winSize.width / 750);
  64. this.node.parent.getChildByName("Ground").setPosition(0, this.groundY);
  65. this.node.parent.getChildByName("StartGround").setPosition(0, -cc.winSize.height / 2);
  66. this.startGroundY = this.node.parent.getChildByName("StartGround").y;
  67. },
  68. initBuding: function () {
  69. this.colorType = Math.floor(Math.random() * 4);
  70. this.node.setPosition(cc.p(0, -200));
  71. this.skSprite.setPosition(cc.p(0, -200));
  72. this.node.parent.getChildByName("Ground").setPosition(0, this.groundY);
  73. var skintype = UserInfo.getSkinIndex();
  74. var self = this;
  75. cc.loader.loadRes(cfg.buDingTypePath[skintype][self.colorType], cc.SpriteFrame, function (err, spriteFrame) {
  76. self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame;
  77. self.node.active = true;
  78. self.currentSpeed = 0;
  79. self.state = State.Drop;
  80. self.skSprite.active = true;
  81. self.skSprite.getComponent("sp.Skeleton").setSkin("p" + (skintype + 1));
  82. self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[self.colorType][self.state], true);
  83. })
  84. },
  85. setSkSpriteVisible: function (bVisible) {
  86. this.skSprite.active = bVisible;
  87. },
  88. stopBuding: function () {
  89. this.state = State.None;
  90. this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true);
  91. },
  92. resumeBuding: function () {
  93. this.state = State.Drop;
  94. this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true);
  95. },
  96. setColorType: function (colorType) {
  97. this.colorType = colorType;
  98. },
  99. jump: function () {
  100. if (this.state != State.Dead) {
  101. this.state = State.Jump;
  102. this.currentSpeed = this.initJumpSpeed;
  103. //-- 播放跳音效
  104. cc.audioEngine.playEffect(this.jumpAudio);
  105. }
  106. //this.spawnDust('DustUp');
  107. },
  108. changeColor: function (colorType) {
  109. this.colorType = colorType;
  110. var self = this;
  111. var skintype = UserInfo.getSkinIndex();
  112. cc.loader.loadRes(cfg.buDingTypePath[skintype][colorType], cc.SpriteFrame, function (err, spriteFrame) {
  113. self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame;
  114. self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[colorType][self.state], true);
  115. })
  116. },
  117. //碰撞检测
  118. onCollisionEnter: function (other, self) {
  119. console.log('onCollisionEnter');
  120. if (other.tag < 4) {
  121. if (this.colorType == other.tag) {
  122. cc.log("穿过去了");
  123. } else {
  124. cc.log("撞上了");
  125. if (this.shield.active || this.shieldInvincible) {
  126. console.log('护盾无敌时间')
  127. this.removeShield();
  128. return;
  129. }
  130. if (this.state != State.Dead) {
  131. this.state = State.Dead;
  132. this.scheduleOnce(this.scheduleCallback, 1);
  133. }
  134. }
  135. } else if (other.tag == 5) {
  136. cc.log("吃到星星");
  137. other.node.active = false;
  138. other.node.parent.getChildByName("ScoreSkSpine").active = true;
  139. other.node.parent.getChildByName("ScoreSkSpine").getComponent("sp.Skeleton").setAnimation(0, "a1", false);
  140. this.node.parent.getComponent("GameSence").addScore();
  141. } else if (other.tag == 4) {
  142. cc.log("吃到变色块");
  143. other.node.active = false;
  144. var colorType = other.node.parent.getComponent("Obstacle").getColorType();
  145. this.changeColor(colorType);
  146. this.node.parent.getComponent("GameSence").addLayerNum();
  147. this.node.parent.getComponent("GameSence").addScore();
  148. } else if (other.tag == 6) {
  149. cc.log(this.node.parent.getComponent("GameSence"))
  150. cc.log("掉到屏幕外 %d", this.node.parent.getChildByName("Ground").y);
  151. if (this.node.parent.getChildByName("Ground").y > this.groundY) {
  152. if (this.shield.active) {
  153. this.removeShield();
  154. return
  155. }
  156. if (this.state != State.Dead) {
  157. this.state = State.Dead;
  158. this.scheduleOnce(this.scheduleCallback, 1);
  159. }
  160. }
  161. }
  162. },
  163. removeShield() {
  164. this.shield.active = false;
  165. this.shieldInvincible = true;
  166. this.scheduleOnce(() => {
  167. console.log('复原无敌时间')
  168. this.shieldInvincible = false;
  169. }, 1)
  170. },
  171. scheduleCallback: function () {
  172. this.node.parent.getComponent("GameSence").finishGame();
  173. this.state = State.None;
  174. },
  175. update: function (dt) {
  176. switch (this.state) {
  177. case State.Jump:
  178. if (this.currentSpeed < 0) {
  179. this.state = State.Drop;
  180. }
  181. break;
  182. case State.Drop:
  183. if (this.node.y < this.startGroundY + 140) {
  184. this.node.y = this.startGroundY + 140;
  185. this.shield.y = this.node.y + 5;
  186. this.skSprite.setPosition(cc.p(0, this.node.y));
  187. this.state = State.None;
  188. //this.scheduleOnce(this.scheduleCallback,1);
  189. }
  190. break;
  191. case State.Dead:
  192. case State.None:
  193. return;
  194. }
  195. var flying = this.state === State.Jump || this.skSprite.y > this.startGroundY + 50;
  196. if (flying) {
  197. this.currentSpeed -= dt * this.gravity;
  198. this.node.y += dt * this.currentSpeed;
  199. this.shield.y = this.node.y + 5;
  200. this.skSprite.setPosition(cc.p(0, this.node.y));
  201. //地面位置
  202. if (this.skSprite.y > 0 && this.state === State.Jump) {
  203. this.node.parent.getChildByName("Ground").setPosition(0, this.skSprite.y + this.groundY);
  204. }
  205. }
  206. },
  207. });