CoinDrop.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { _decorator, Component, Vec3, tween, find, Label } from 'cc';
  2. import { LevelSessionManager } from '../Core/LevelSessionManager';
  3. const { ccclass } = _decorator;
  4. @ccclass('CoinDrop')
  5. export class CoinDrop extends Component {
  6. private targetWorldPos: Vec3 = new Vec3();
  7. onEnable() {
  8. // 找到 UI 里金币图标的世界坐标
  9. const icon = find('Canvas-001/TopArea/CoinNode/CoinDrop');
  10. if (!icon) {
  11. console.error('[CoinDrop] ❌ 找不到金币图标节点 Canvas-001/TopArea/CoinNode/CoinDrop');
  12. this.node.destroy();
  13. return;
  14. }
  15. icon.getWorldPosition(this.targetWorldPos);
  16. // 初始弹跳向上 80px(0.3s)→ 落下 40px(0.2s)→ 飞向目标 (0.4s)
  17. const start = this.node.worldPosition.clone();
  18. const bounceUp = start.clone(); bounceUp.y += 80;
  19. const settle = bounceUp.clone(); settle.y -= 40;
  20. tween(this.node)
  21. .to(0.3, { worldPosition: bounceUp }, { easing: 'quadOut' })
  22. .to(0.2, { worldPosition: settle }, { easing: 'quadIn' })
  23. .to(0.4, { worldPosition: this.targetWorldPos }, { easing: 'quadIn' })
  24. .call(() => {
  25. // 飞到图标后 +1 并销毁
  26. const lblNode = find('Canvas-001/TopArea/CoinNode/CoinLabel');
  27. if (!lblNode) {
  28. console.error('[CoinDrop] ❌ 找不到金币数字标签节点 Canvas-001/TopArea/CoinNode/CoinLabel');
  29. } else {
  30. const lbl = lblNode.getComponent(Label);
  31. if (lbl) {
  32. lbl.string = (parseInt(lbl.string) + 1).toString();
  33. } else {
  34. console.error('[CoinDrop] ❌ CoinLabel 节点缺少 Label 组件');
  35. }
  36. }
  37. LevelSessionManager.inst.addCoins(1); // 记账(局内金币)
  38. this.node.destroy();
  39. })
  40. .start();
  41. }
  42. }