import { _decorator, Component, Node, RigidBody2D, UITransform, Vec2 } from 'cc'; import { ObjectPoolManager } from '../ObjectPoolManager'; const { ccclass, property } = _decorator; @ccclass('PropBase') export class PropBase extends Component { private _UpSpeed: number = 50; public get UpSpeed(): number { return this._UpSpeed; } public set UpSpeed(value: number) { this._UpSpeed = value; this.setSpeed() } ParentSize: any setSpeed() { let rigidBody = this.getComponent(RigidBody2D) if (rigidBody) { // 设置向上的速度 rigidBody.linearVelocity = new Vec2(0, this.UpSpeed); // 速度值可以根据需要调整 } else { console.error('RigidBody2D component not found!'); } } updateTime: number; update(deltaTime: number) { this.updateTime += deltaTime; let _time = 1 / 60//60FPS if (this.updateTime < _time) return; this.updateTime = this.updateTime - _time; this.FixUpdate() } FixUpdate() { let height = this.ParentSize.height / 2; height += 50 if (this.node.getPosition().y > height) { ObjectPoolManager.getInstance().putNodeToPool(this.node) } } unuse() { } reuse(ParentNode: Node) { ParentNode[0].addChild(this.node); let size = ParentNode[0].getComponent(UITransform).contentSize this.ParentSize = size let width = (size.width - (this.node.getComponent(UITransform).contentSize.width * 1.33)) / 2; let height = -size.height / 2; height -= 50; const x = this.getRandomNumber(-width, width); const y = height this.node.setPosition(x, y, 0); this.node.getComponent(PropBase).UpSpeed = 15 } getRandomNumber(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; } }