BuDingSprite.js 7.2 KB

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