ReelEffect.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Component, instantiate, Node, UITransform, Vec3, view, _decorator } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('ReelEffect')
  4. export class ReelEffect extends Component {
  5. speed: number = 15;
  6. moutainNode1: Node;
  7. moutainNode2: Node;
  8. moutainTf: UITransform;
  9. posXOut: number;
  10. tempPos: Vec3 = new Vec3();
  11. onLoad() {
  12. this.moutainNode1 = this.node.children[0];
  13. this.moutainNode2 = instantiate(this.moutainNode1);
  14. this.moutainNode2.parent = this.node;
  15. this.moutainTf = this.moutainNode1.getComponent(UITransform);
  16. this.moutainNode1.getPosition(this.tempPos);
  17. this.tempPos.x = this.tempPos.x - this.moutainTf.width;
  18. this.moutainNode2.setPosition(this.tempPos);
  19. this.posXOut = view.getVisibleSize().width * 0.5 + this.moutainTf.width * 0.5;
  20. }
  21. addPosX(node: Node, dx: number) {
  22. node.getPosition(this.tempPos);
  23. this.tempPos.x += dx;
  24. node.setPosition(this.tempPos);
  25. }
  26. update(dt: number) {
  27. let dx = dt * this.speed;
  28. this.addPosX(this.moutainNode1, dx);
  29. this.addPosX(this.moutainNode2, dx);
  30. this.moutainNode1.getPosition(this.tempPos);
  31. if (this.tempPos.x > this.posXOut) {
  32. this.moutainNode2.getPosition(this.tempPos);
  33. this.tempPos.x = this.tempPos.x - this.moutainTf.width;
  34. this.moutainNode1.setPosition(this.tempPos);
  35. }
  36. this.moutainNode2.getPosition(this.tempPos);
  37. if (this.tempPos.x > this.posXOut) {
  38. this.moutainNode1.getPosition(this.tempPos);
  39. this.tempPos.x = this.tempPos.x - this.moutainTf.width;
  40. this.moutainNode2.setPosition(this.tempPos);
  41. }
  42. }
  43. }