import { _decorator, BoxCollider2D, Component, Node, PhysicsSystem2D, UITransform } from 'cc'; const { ccclass, property } = _decorator; @ccclass('PushItem') export class PushItem extends Component { targetWidth: number = 500; //duration: number = 60.0; private speed: number = 2; start() { // this.init() } init(time) { let boxCollider2D = this.getComponent(BoxCollider2D) const startWidth = this.getComponent(UITransform).contentSize.width; const deltaWidth = this.targetWidth - startWidth; if (!time) { time = 1 } // 计算每秒需要增加的宽度 this.speed = deltaWidth / time; // let s = endPos.pushItem - this.getComponent(UITransform).contentSize.width; // this.speed = s / time } resize(dt: number) { // 计算这一帧需要增加的宽度 const deltaWidth = this.speed * dt; // 获取当前宽度 const uit = this.getComponent(UITransform); const currentWidth = uit.contentSize.width; // 更新宽度 const newWidth = currentWidth + deltaWidth; // 如果已经达到目标宽度,停止更新 if (newWidth >= this.targetWidth) { this.speed = 0; // 停止更新 uit.setContentSize(this.targetWidth, uit.contentSize.height); } else { uit.setContentSize(newWidth, uit.contentSize.height); } this.updateCollider(newWidth); } //更新碰撞体 updateCollider(width: number) { const collider = this.getComponent(BoxCollider2D); if (collider) { collider.size.width = width - 10; collider.offset.x = (width - 10) * 0.5; collider.apply(); } } }