import { _decorator, Component, Vec3, tween, find, Label } from 'cc'; import { LevelSessionManager } from '../Core/LevelSessionManager'; const { ccclass } = _decorator; @ccclass('CoinDrop') export class CoinDrop extends Component { private targetWorldPos: Vec3 = new Vec3(); onEnable() { // 找到 UI 里金币图标的世界坐标 const icon = find('Canvas/GameLevelUI/CoinNode/CoinDrop'); if (!icon) { this.node.destroy(); return; } icon.getWorldPosition(this.targetWorldPos); // 初始弹跳向上 80px(0.3s)→ 落下 40px(0.2s)→ 飞向目标 (0.4s) const start = this.node.worldPosition.clone(); const bounceUp = start.clone(); bounceUp.y += 80; const settle = bounceUp.clone(); settle.y -= 40; tween(this.node) .to(0.3, { worldPosition: bounceUp }, { easing: 'quadOut' }) .to(0.2, { worldPosition: settle }, { easing: 'quadIn' }) .to(0.4, { worldPosition: this.targetWorldPos }, { easing: 'quadIn' }) .call(() => { // 飞到图标后 +1 并销毁 const lblNode = find('Canvas/GameLevelUI/CoinNode/CoinLabel'); const lbl = lblNode?.getComponent(Label); if (lbl) { lbl.string = (parseInt(lbl.string) + 1).toString(); } LevelSessionManager.inst.addCoins(1); // 记账(局内金币) this.node.destroy(); }) .start(); } }