import { Animation, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Prefab, UITransform, Vec2, _decorator, v2, v3 } from 'cc'; import { GameManager } from '../../Manager/GameManager'; import { AudioManage } from '../../Manager/AudioManage'; import { Global } from '../../Common/Global'; const { ccclass, property } = _decorator; @ccclass('Guaishou') export class Guaishou extends Component { private _moveDirection: Vec2 | undefined; private collider; onLoad() { this.move(); this.collider = this.node.getComponent(BoxCollider2D); if (this.collider) { // this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); } } public move() { this._moveDirection = v2(-1, 0); } //碰撞事件 onBeginContact(hero_collier: Collider2D, box_collier: Collider2D, contact: IPhysics2DContact | null) { console.log("怪兽碰撞") } update(deltaTime: number) { if (!this._moveDirection || !this.node.parent) return; if(!GameManager.getInstance().isStartGame) return; // 计算移动位置 this.node.translate(v3(this._moveDirection.x * GameManager.getInstance().speed * deltaTime, 0, 0)); if (this.node.position.x <= -500) { this._moveDirection = null; let anim = this.node.getChildByName("guaishou").getComponent(Animation); anim.play("gongji"); anim.on(Animation.EventType.FINISHED, (Event, state) => { GameManager.getInstance().minusScore(GameManager.getInstance().minusscoreNum); AudioManage.instance.playSound(Global.Audio_MinusScore); this.node.removeFromParent(); this.destroy(); }); } if (this.node.position.x <= -850) { this.node.removeFromParent(); this.destroy(); } } }