HeadShotTipComponent.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { _decorator, Component, Game, Node, tween, UIOpacity } from 'cc';
  2. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  3. import { GameEvent } from '../Enum/GameEvent';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('HeadTipComponent')
  6. export class HeadTipComponent extends Component {
  7. private _children: Node[] = [];
  8. protected onLoad(): void {
  9. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_HEADSHOT,this.showTip,this);
  10. }
  11. start() {
  12. // 获取所有子节点
  13. this._children = this.node.children;
  14. // 初始隐藏所有提示
  15. this._children.forEach(child => child.active = false);
  16. }
  17. showTip() {
  18. if (this._children.length < 2) return;
  19. // 随机选择一个子节点
  20. const randomIndex = Math.floor(Math.random() * this._children.length);
  21. const tipNode = this._children[randomIndex];
  22. // 显示节点并添加淡出效果
  23. tipNode.active = true;
  24. const uiOpacity = tipNode.getComponent(UIOpacity) || tipNode.addComponent(UIOpacity);
  25. uiOpacity.opacity = 255;
  26. tween(uiOpacity)
  27. .delay(0.5)
  28. .to(0.3, { opacity: 0 })
  29. .call(() => {
  30. tipNode.active = false;
  31. })
  32. .start();
  33. }
  34. update(deltaTime: number) {
  35. }
  36. }