63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { _decorator, Component, Node,Collider2D ,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('bossCom')
|
|
export class bossCom extends Component {
|
|
@property(Number)
|
|
private speed: number = 100;
|
|
@property(Number)
|
|
private ospeed: number = 100;
|
|
@property(Number)
|
|
private timer: number = 0;
|
|
|
|
protected onLoad(): void {
|
|
this.speed = this.ospeed = 100;
|
|
}
|
|
start () {
|
|
// 注册单个碰撞体的回调函数
|
|
// console.log('MonsterViewComp start');
|
|
let collider = this.getComponent(Collider2D);
|
|
// console.log('Monster collider',collider);
|
|
if (collider) {
|
|
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
|
}
|
|
|
|
// 注册全局碰撞回调函数
|
|
if (PhysicsSystem2D.instance) {
|
|
// console.log('PhysicsSystem2D.instance');
|
|
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
|
}
|
|
}
|
|
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
// 只在两个碰撞体开始接触时被调用一次
|
|
// console.log('monster Contact,otherCollider',otherCollider);
|
|
this.speed = 0;
|
|
this.timer = 1;
|
|
|
|
}
|
|
|
|
update(dt: number){
|
|
if(this.timer > 0){
|
|
this.timer -= dt;
|
|
if(this.timer <= 0){
|
|
this.speed = this.ospeed;
|
|
this.timer = 0;
|
|
}
|
|
}
|
|
if(this.node.position.x > -360){
|
|
this.move(dt);
|
|
}
|
|
if(this.node.position.x < -360){
|
|
|
|
this.node.destroy();
|
|
}
|
|
|
|
|
|
}
|
|
move(dt: number){
|
|
this.node.setPosition(this.node.position.x-dt*this.speed, this.node.position.y, this.node.position.z);
|
|
}
|
|
}
|
|
|
|
|