ZhuanKuaiItem.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { _decorator, BoxCollider2D, Collider2D, Component, Contact2DType, game, Node, tween, UIOpacity } from 'cc';
  2. import { GameManager } from '../Manager/GameManager';
  3. import { AudioManage } from '../Manager/AudioManage';
  4. import { Global } from '../Common/Global';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('ZhuanKuaiItem')
  7. export class ZhuanKuaiItem extends Component {
  8. @property(BoxCollider2D)
  9. topNode: BoxCollider2D = null;
  10. @property(BoxCollider2D)
  11. bottomNode: BoxCollider2D = null;
  12. @property(BoxCollider2D)
  13. leftNode: BoxCollider2D = null;
  14. @property(BoxCollider2D)
  15. rightNode: BoxCollider2D = null;
  16. start() {
  17. this.topNode.on(Contact2DType.BEGIN_CONTACT, (() => {
  18. console.log("碰到砖块上方!");
  19. this.gamemain.changeAngle(1);
  20. this.onHide();
  21. }));
  22. this.rightNode.on(Contact2DType.BEGIN_CONTACT, (() => {
  23. console.log("碰到砖块右方!");
  24. this.gamemain.changeAngle(2);
  25. this.onHide();
  26. }));
  27. this.leftNode.on(Contact2DType.BEGIN_CONTACT, (() => {
  28. console.log("碰到砖块左方!");
  29. this.gamemain.changeAngle(3);
  30. this.onHide();
  31. }));
  32. this.bottomNode.on(Contact2DType.BEGIN_CONTACT, (() => {
  33. console.log("碰到砖块下方!");
  34. this.gamemain.changeAngle(4);
  35. this.onHide();
  36. }));
  37. }
  38. private gamemain;
  39. private isAddScore = true;
  40. initUI(gamemain) {
  41. this.gamemain = gamemain;
  42. }
  43. private onHide() {
  44. if (!GameManager.getInstance().isStartGame) return
  45. if (GameManager.getInstance().isPaused) return
  46. if (this.isAddScore) {
  47. this.isAddScore = false;
  48. GameManager.getInstance().addScore(this.gamemain.score);
  49. if (GameManager.getInstance().curLevelScore >= GameManager.getInstance().curLevelData.targetScore) {
  50. this.gamemain.isMove = false;
  51. }
  52. // AudioManage.instance.playSound(Global.Audio_AddScore);
  53. tween(this.node.getComponent(UIOpacity))
  54. .to(0.03, { opacity: 0 })
  55. .to(0.03, { opacity: 255 })
  56. .call(() => {
  57. this.node.removeFromParent();
  58. })
  59. .start();
  60. }
  61. }
  62. update(deltaTime: number) {
  63. }
  64. }