BloodProgressBar.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. cc.Class({
  2. extends: cc.Component,
  3. // properties: {
  4. //
  5. // },
  6. // LIFE-CYCLE CALLBACKS:
  7. onLoad () {
  8. this.totalNum = 2000; //boss的血量
  9. this.myBlood = 2000; //玩家的血量
  10. },
  11. start () {
  12. //获取boss的进度条bar
  13. this.bossBarNode = cc.find("Canvas/BloodProgress/bar").getComponent(cc.Sprite);
  14. //获取EffectLayer组件
  15. this.effNum = cc.find("Canvas/GameScene/EffectLayer").getComponent("EffectLayer");
  16. //获取currentNums的Label的值
  17. this.currentNum = this.effNum.currentNums.getComponent(cc.Label).string;
  18. //获取已使用的总步数
  19. this.stepSurplus = this.effNum.stepTotal;
  20. //获取关卡数据
  21. this.levelNum = this.effNum.curLevelNum;
  22. //获取boss图片
  23. this.bossImg = cc.find("Canvas/BloodProgress/boss");
  24. //获取攻击武器的节点
  25. this.bossAttackNode = cc.find("Canvas/BloodProgress/bossAttackEffect")
  26. //获取玩家的血量节点
  27. this.myBloodNode = cc.find("Canvas/BloodProgress/myBar").getComponent(cc.Sprite);
  28. },
  29. // update (dt) {
  30. // // console.log("this.bossBarNode.fillRange:",this.bossBarNode.fillRange)
  31. // // console.log("this.myBloodNode.fillRange:",this.myBloodNode.fillRange)
  32. // },
  33. //boss受到攻击
  34. bossProgress(){
  35. //boss进度条的血量
  36. let per = (this.currentNum / this.totalNum).toFixed(2);
  37. per = (per > 1) ? 1 : per;
  38. this.progressBarFillRange = 1 - per; // 1 - per 表示倒计时
  39. // console.log("this.progressBarFillRange:",this.progressBarFillRange)
  40. //更新进度条的值
  41. this.bossBarNode.fillRange = this.progressBarFillRange;
  42. //boss被攻击后的动画效果
  43. let rotateRight = cc.rotateBy(0.06, 30);
  44. let rotateLeft = cc.rotateBy(0.12, -60);
  45. let sequence = cc.sequence(rotateRight, rotateLeft, rotateRight);
  46. this.bossImg.runAction(sequence);
  47. // console.log("完成一个动画");
  48. //判断是否闯关成功
  49. if (this.bossBarNode.fillRange == 0 && this.myBloodNode.fillRange > 0){
  50. // console.log("闯关成功")
  51. // console.log("this.levelNum2:",this.levelNum)
  52. //闯关成功,获取弹出层
  53. let resultMask = cc.find("Canvas/Result")
  54. //展示关卡
  55. resultMask.getChildByName("Title").getComponent(cc.Label).string = `第${this.levelNum}关完成`;
  56. //展示当前分数
  57. resultMask.getChildByName("ScoreSum").getChildByName("ScoreDetail").getComponent(cc.Label).string = this.currentNum;
  58. //展示金币
  59. resultMask.getChildByName("Golden").getChildByName("GoldenDetail").getComponent(cc.Label).string = 20;
  60. //展示星星
  61. resultMask.getChildByName("Star").getChildByName("StarDetail").getComponent(cc.Label).string = 20;
  62. //显示弹出层
  63. resultMask.active = true;
  64. //通关后,关卡+1
  65. this.effNum.curLevelNum++;
  66. //如果关卡大于2,剩余步数设为28,否则为20
  67. // if (this.curLevelNum > 2){
  68. // this.surplusStepNums.string = 28;
  69. // }else {
  70. // this.surplusStepNums.string = 20;
  71. // }
  72. //重置
  73. this.bossBarNode.fillRange = 1; //通关后boss血条进度设为1
  74. this.myBloodNode.fillRange = 1; //通关后玩家血条进度设为1
  75. this.effNum.stepTotal = 0; //通关后步数清零
  76. this.effNum.currentNums.getComponent(cc.Label).string = 0; //通关后当前分数设为0
  77. this.stepSurplus = 0; //已消耗的步数设为0
  78. cc.find("Canvas/TopResult").active = false; //顶部区域不显示
  79. cc.find("Canvas/BloodProgress").active = false; //血条不显示
  80. }
  81. },
  82. //boss攻击玩家
  83. bossAttack(){
  84. //获取攻击武器的节点设置为显示状态
  85. this.bossAttackNode.active = true;
  86. //设置攻击武器的起始位置
  87. this.bossAttackNode.x = 70;
  88. //设置攻击武器移动向右移动
  89. let right = cc.moveTo(0.3,cc.v2(220,-70));
  90. //移动动作完成后的回调
  91. let finished = cc.callFunc(function (){
  92. // console.log("动作完成回调");
  93. //获取攻击武器的节点
  94. this.bossAttackNode = cc.find("Canvas/BloodProgress/bossAttackEffect")
  95. //获取攻击武器的节点设置为隐藏示状态
  96. this.bossAttackNode.active = false;
  97. //获取玩家血量的节点
  98. this.myBloodNode = cc.find("Canvas/BloodProgress/myBar").getComponent(cc.Sprite);
  99. //玩家的血量
  100. let per = (this.stepSurplus * 80 / this.myBlood).toFixed(2);
  101. per = (per > 1) ? 1 : per;
  102. this.myBloodNodeFillRange = 1 - per; // 1 - per 表示倒计时
  103. // console.log("this.myBloodNodeFillRange:",this.myBloodNodeFillRange)
  104. //更新进度条的值
  105. this.myBloodNode.fillRange = this.myBloodNodeFillRange;
  106. },this);
  107. //定义一个序列动画
  108. let sequence = cc.sequence(right,finished);
  109. this.bossAttackNode.runAction(sequence);
  110. //是否闯关失败的判断
  111. if (this.bossBarNode.fillRange > 0 && this.myBloodNode.fillRange <= (80 / this.myBlood)) {
  112. //闯关失败,获取弹出层
  113. let resultMask = cc.find("Canvas/Result")
  114. //展示关卡
  115. resultMask.getChildByName("Title").getComponent(cc.Label).string = "差一点点就成功了哦";
  116. //展示当前分数
  117. resultMask.getChildByName("ScoreSum").getChildByName("ScoreDetail").getComponent(cc.Label).string = this.currentNum;
  118. //展示金币
  119. resultMask.getChildByName("Golden").getChildByName("GoldenDetail").getComponent(cc.Label).string = 0;
  120. //展示星星
  121. resultMask.getChildByName("Star").getChildByName("StarDetail").getComponent(cc.Label).string = 0;
  122. //显示弹出层
  123. resultMask.active = true;
  124. //闯关失败,不展示去动物园按钮
  125. let zoom = cc.find("Canvas/Result/BtnLayout/ToZoom")
  126. zoom.active = false;
  127. //如果关卡大于2,剩余步数设为28,否则为20
  128. // if (this.curLevelNum > 2){
  129. // this.surplusStepNums.string = 28;
  130. // }else {
  131. // this.surplusStepNums.string = 20;
  132. // }
  133. //重置
  134. this.bossBarNode.fillRange = 1; //boss血条进度设为1
  135. this.myBloodNode.fillRange = 1; //玩家血条进度设为1
  136. this.effNum.stepTotal = 0; //步数清零
  137. this.effNum.currentNums.getComponent(cc.Label).string = 0; //当前分数设为0
  138. this.stepSurplus = 0; //已消耗步数设为0
  139. cc.find("Canvas/TopResult").active = false; //顶部区域不显示
  140. cc.find("Canvas/BloodProgress").active = false; //血条不显示
  141. }
  142. // console.log("boss攻击了玩家");
  143. },
  144. });