| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 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-001/TopArea/CoinNode/CoinDrop');
- if (!icon) {
- console.error('[CoinDrop] ❌ 找不到金币图标节点 Canvas-001/TopArea/CoinNode/CoinDrop');
- 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-001/TopArea/CoinNode/CoinLabel');
- if (!lblNode) {
- console.error('[CoinDrop] ❌ 找不到金币数字标签节点 Canvas-001/TopArea/CoinNode/CoinLabel');
- } else {
- const lbl = lblNode.getComponent(Label);
- if (lbl) {
- lbl.string = (parseInt(lbl.string) + 1).toString();
- } else {
- console.error('[CoinDrop] ❌ CoinLabel 节点缺少 Label 组件');
- }
- }
- LevelSessionManager.inst.addCoins(1); // 记账(局内金币)
- this.node.destroy();
- })
- .start();
- }
- }
|