/** * @file SmallTipComp.ts * @description smalltip 气泡提示组件(UI 通用层) * * 职责: * 封装 smalltip.prefab 气泡的弹出/消失动画与文案填充,供各页面复用。 * 调用方通过 getComponent(SmallTipComp) 获取实例后调 show(text) 即可, * 无需关心节点显隐与 Tween 生命周期。 * * 用法: * 1. 编辑器侧将本组件挂在 smalltip 节点上(或运行时 addComponent 兜底)。 * 2. 默认隐藏节点(active = false),调用 show() 自动弹出并在停留后缩小隐藏。 */ import { _decorator, Component, Label, Tween, Vec3, tween } from "cc"; const { ccclass } = _decorator; /** 气泡停留时长(秒) */ const STAY_DURATION = 1; /** SmallTipComp —— smalltip 气泡提示(弹出 → 停留 → 缩小隐藏) */ @ccclass('SmallTipComp') export class SmallTipComp extends Component { protected onLoad(): void { // 初始隐藏,避免遮挡挂载点的触摸事件 this.node.active = false; } /** * 弹出气泡提示(重复调用时打断当前动画,从头播放) * @param text 提示文案 * @param stay 停留时长(秒),默认 1 秒 */ public show(text: string, stay: number = STAY_DURATION): void { // 覆盖气泡文字 const label = this.getComponentInChildren(Label); if (label) label.string = text; this.node.active = true; Tween.stopAllByTarget(this.node); this.node.setScale(0, 0, 1); tween(this.node) // 弹出动画(微放大再回弹) .to(0.15, { scale: new Vec3(1.1, 1.1, 1) }, { easing: 'quadOut' }) .to(0.05, { scale: new Vec3(1, 1, 1) }) // 停留 .delay(stay) // 缩小消失 .to(0.15, { scale: new Vec3(0, 0, 1) }, { easing: 'quadIn' }) .call(() => { if (this.node && this.node.isValid) this.node.active = false; }) .start(); } protected onDestroy(): void { Tween.stopAllByTarget(this.node); } }