Zidan.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Prefab, UITransform, Vec2, _decorator, v2, v3 } from 'cc';
  2. import { GameManager } from '../../Manager/GameManager';
  3. import { Global } from '../../Common/Global';
  4. import { AudioManage } from '../../Manager/AudioManage';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('Zidan')
  7. export class Zidan 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. GameManager.getInstance().addScore(GameManager.getInstance().addscoreNum)
  24. AudioManage.instance.playSound(Global.Audio_AddScore);
  25. hero_collier.node.removeFromParent();
  26. hero_collier.node.destroy();
  27. box_collier.node.removeFromParent();
  28. box_collier.node.destroy();
  29. }
  30. update(deltaTime: number) {
  31. if (!this._moveDirection || !this.node.parent) return;
  32. if(!GameManager.getInstance().isStartGame)
  33. return;
  34. // 计算移动位置
  35. this.node.translate(v3(this._moveDirection.x * 1000 * deltaTime, 0, 0));
  36. if (this.node.position.x > this.node.parent.getComponent(UITransform).contentSize.width / 2 - this.node.getComponent(UITransform).width / 2) {
  37. this.node.removeFromParent();
  38. this.destroy();
  39. }
  40. }
  41. }