FloatTextNode.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, Color, Font, Label, Node, tween, UIOpacity, UITransform, v3, Vec3 } from 'cc';
  2. import { NodeEx } from '../../framework/pool/NodeEx';
  3. import { FloatTextPool } from '../common/Pool';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('FloatTextNode')
  6. export class FloatTextNode extends NodeEx {
  7. protected onLoad() {
  8. }
  9. //
  10. init(pic, text, scale = 1, color = new Color('#ffffff'), direction = 1, speed = 0.3,) {
  11. // scale = 1
  12. this.node.getComponent(Label).string = '';
  13. let PosX = 0;
  14. let poxY = 100;
  15. if (typeof text == 'number') {
  16. this.node.setPosition(new Vec3(0, 0))
  17. this.node.getComponent(Label).string = (text > 0) ? '+' + text : text + '';
  18. } else {
  19. poxY = 200;
  20. PosX = PosX * direction;
  21. this.node.setPosition(new Vec3(PosX, 0))
  22. this.node.getComponent(Label).string = text;
  23. speed = 0.5
  24. this.node.getComponent(Label).fontSize = 20;
  25. // this.node.scale = new Vec3(0.3, 0.3, 1)
  26. }
  27. this.node.active = true;
  28. if (pic) {
  29. this.Sprite.State.spriteFrame = pic
  30. this.Sprite.State.node.setPosition(new Vec3(-(this.Sprite.State.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width), 1.2))
  31. }
  32. this.node.getComponent(Label).color = color;
  33. // tween(this.node.getComponent(UIOpacity)).to(speed, { opacity: 0 }).start();
  34. tween(this.node).by(0.1, { scale: new Vec3(2.2, 2.2) }).to(speed, { position: v3(PosX, poxY) }).call(() => {
  35. this.node.scale = new Vec3(1, 1, 1)
  36. this.node.getComponent(UIOpacity).opacity = 255;
  37. this.node.active = false;
  38. FloatTextPool.putNode(this);
  39. }).start();
  40. }
  41. protected onDestroy() {
  42. //如果该组件有事件自行取消注释
  43. //Framework.event.removeEvent(this);
  44. super.onDestroy();
  45. }
  46. //节点被放入池中时调用
  47. unuse() {
  48. }
  49. //节点从池中取出时调用
  50. reuse(...val: any[]) {
  51. }
  52. }