Role.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { GameMain } from "./GameMain";
  2. import { _decorator, Component, Node, Collider2D, Contact2DType, IPhysics2DContact, PhysicsSystem2D, RigidBody2D } from 'cc';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('Role')
  5. export class Role extends Component {
  6. @property(GameMain)
  7. main: GameMain = null;//获取main脚本
  8. start() {
  9. // 注册单个碰撞体的回调函数
  10. let collider = this.getComponent(Collider2D);
  11. if (collider) {
  12. console.log('onBeginContact Role');
  13. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  14. }
  15. }
  16. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  17. // 只在两个碰撞体开始接触时被调用一次
  18. console.log('onBeginContact=' + otherCollider.node.name);
  19. //
  20. if (otherCollider.tag == 0) {
  21. if (this.node.name == "Role1") {
  22. this.main.GameOver(1);//游戏结束
  23. }
  24. else if (this.node.name == "Role2") {
  25. this.main.GameOver(2);//游戏结束
  26. }
  27. }
  28. }
  29. }