Guaishou.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Animation, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Prefab, UITransform, Vec2, _decorator, v2, v3 } 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('Guaishou')
  7. export class Guaishou extends Component {
  8. private _moveDirection: Vec2 | undefined;
  9. private collider;
  10. onLoad() {
  11. this.move();
  12. this.collider = this.node.getComponent(BoxCollider2D);
  13. if (this.collider) {
  14. // this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  15. }
  16. }
  17. public move() {
  18. this._moveDirection = v2(-1, 0);
  19. }
  20. //碰撞事件
  21. onBeginContact(hero_collier: Collider2D, box_collier: Collider2D, contact: IPhysics2DContact | null) {
  22. console.log("怪兽碰撞")
  23. }
  24. update(deltaTime: number) {
  25. if (!this._moveDirection || !this.node.parent) return;
  26. if(!GameManager.getInstance().isStartGame)
  27. return;
  28. // 计算移动位置
  29. this.node.translate(v3(this._moveDirection.x * GameManager.getInstance().speed * deltaTime, 0, 0));
  30. if (this.node.position.x <= -500) {
  31. this._moveDirection = null;
  32. let anim = this.node.getChildByName("guaishou").getComponent(Animation);
  33. anim.play("gongji");
  34. anim.on(Animation.EventType.FINISHED, (Event, state) => {
  35. GameManager.getInstance().minusScore(GameManager.getInstance().minusscoreNum);
  36. AudioManage.instance.playSound(Global.Audio_MinusScore);
  37. this.node.removeFromParent();
  38. this.destroy();
  39. });
  40. }
  41. if (this.node.position.x <= -850) {
  42. this.node.removeFromParent();
  43. this.destroy();
  44. }
  45. }
  46. }