FlutterEffect.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Component, UITransform, Vec3, view, _decorator } from 'cc';
  2. import { utilTools } from '../../../utils/utilTools';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('FlutterEffect')
  5. export class FlutterEffect extends Component {
  6. // 指定速度,不指定将会随机
  7. speed: number = 0;
  8. speedArr: any = [40, 50, 60];
  9. tempPos: Vec3 = new Vec3();
  10. onLoad() {
  11. }
  12. start() {
  13. let visibleSize = view.getVisibleSize();
  14. let chs = this.node.children;
  15. for (let i in chs) {
  16. let node = chs[i];
  17. if (this.speed > 0) {
  18. // 指定速度
  19. node["speed"] = this.speed;
  20. } else {
  21. // 随机分配速度
  22. node["speed"] = utilTools.getRandomItemByArr(this.speedArr);
  23. }
  24. // 移动目标位置
  25. node["posX"] = visibleSize.width * 0.5 + node.getComponent(UITransform).width * 0.5;
  26. }
  27. }
  28. update(dt: number) {
  29. let chs = this.node.children;
  30. for (let i in chs) {
  31. let node = chs[i];
  32. node.getPosition(this.tempPos);
  33. if (this.tempPos.x > node["posX"]) {
  34. this.tempPos.x = -node["posX"];
  35. }
  36. this.tempPos.x += (dt * node["speed"]);
  37. node.setPosition(this.tempPos);
  38. }
  39. }
  40. }