Playcontroler.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import Global from "./Global";
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class Playcontroler extends cc.Component {
  5. // LIFE-CYCLE CALLBACKS:
  6. // parent:cc.Node = null;
  7. MainScene: cc.Component = null;
  8. onLoad() {
  9. cc.game.setFrameRate(59)
  10. // this.parent = this.node.parent;
  11. this.MainScene = Global.instance.getMN();
  12. }
  13. start() {
  14. }
  15. update(dt) {
  16. if (Global.instance.OverFlag) {
  17. this.enabled = false;
  18. }
  19. else {
  20. if (!Global.instance.CollisionFlag) {
  21. this.node.y -= Global.instance.InitSpeed;
  22. }
  23. else {
  24. let name = Global.instance.TheHolder.name;
  25. if (name == "") {
  26. return;
  27. }
  28. this.node.y = Global.instance.TheHolder.y
  29. + Global.instance.TheHolder.getComponent(name).NodeH - 5;//here ,this way isnot a good Processing method,should be optimizated
  30. }
  31. }
  32. if (Global.instance.CollisionWithDing) {
  33. this.node.stopAllActions();
  34. Global.instance.CollisionWithDing = false;
  35. Global.instance.CollisionFlag = false;
  36. }
  37. }
  38. /**
  39. * player与墙壁的碰撞检测
  40. * @param other 被撞物体
  41. * @param self 碰撞体
  42. */
  43. onCollisionEnter(other, self) {
  44. let rootSelf = this;
  45. switch (other.node.name) {
  46. case "Bg_0CollisionR": {
  47. self.node.x = 180;
  48. break;
  49. }
  50. case "Bg_0CollisionL": {
  51. self.node.x = -180;
  52. break;
  53. }
  54. case "Bg_1CollisionR": {
  55. self.node.x = 180;
  56. break;
  57. }
  58. case "Bg_1CollisionL": {
  59. self.node.x = -180;
  60. break;
  61. }
  62. // case "tanhuang":{
  63. // if(other.node.x+75>self.node.x&&other.node.x-75<self.node.x){
  64. // let main = Global.instance.getMN();
  65. // main.getChildByName("output").getComponent(cc.Label).string = "tanhuang";
  66. // }
  67. // }
  68. case "ding": {
  69. Global.instance.CollisionWithDing = true;
  70. self.node.stopAllActions();
  71. rootSelf.LifeZero();
  72. Global.instance.CollisionFlag = false;
  73. self.node.getComponent(cc.BoxCollider).enabled = false;
  74. rootSelf.scheduleOnce(function (err) {
  75. self.node.getComponent(cc.BoxCollider).enabled = true;
  76. }, 0.3);
  77. break;
  78. }
  79. default: {
  80. // this.MainScene.getComponent("MainScene").Score();//得分
  81. break;
  82. }
  83. }
  84. }
  85. LifeZero() {
  86. let main = Global.instance.getMN();
  87. let lifeChil = main.getChildByName("BgNode").getChildByName("LifeDing").children;
  88. let life = new Array();
  89. let lifeNum = 0;
  90. for (let i = 0; i < lifeChil.length; i++) {
  91. if (lifeChil[i].name == "lifeBG") {
  92. life.push(lifeChil[i]);
  93. }
  94. }
  95. for (let i = 0; i < life.length; i++) {
  96. if (life[i].active) {
  97. lifeNum++;
  98. }
  99. }
  100. if (lifeNum == 0) {
  101. return;
  102. }
  103. }
  104. }
  105. /**
  106. * 1.下落速度加快;
  107. * 2.玻璃:先停一会在碎掉;
  108. * 3.传送带速度快一点;
  109. * 4.
  110. */