import { GameMain } from "./GameMain"; import { _decorator, Component, Node, Collider2D, Contact2DType, IPhysics2DContact, PhysicsSystem2D, RigidBody2D } from 'cc'; const { ccclass, property } = _decorator; @ccclass('Role') export class Role extends Component { @property(GameMain) main: GameMain = null;//获取main脚本 start() { // 注册单个碰撞体的回调函数 let collider = this.getComponent(Collider2D); if (collider) { console.log('onBeginContact Role'); collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); } } onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) { // 只在两个碰撞体开始接触时被调用一次 console.log('onBeginContact=' + otherCollider.node.name); // if (otherCollider.tag == 0) { if (this.node.name == "Role1") { this.main.GameOver(1);//游戏结束 } else if (this.node.name == "Role2") { this.main.GameOver(2);//游戏结束 } } } }