moving-scene-bg.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('game_manger')
  4. export class game_manger extends Component {
  5. @property(Node)
  6. bg01: Node = null;
  7. @property(Node)
  8. bg02: Node = null;
  9. // 移动速度
  10. private _bgSpeed = 10;
  11. // 偏移量
  12. private _bgMovingRange = 50;
  13. onLoad() {
  14. }
  15. start() {
  16. this._init()
  17. }
  18. update(deltaTime: number) {
  19. this._moveBackground(deltaTime)
  20. }
  21. private _init() {
  22. this.bg01.setPosition(0, 0, 0);
  23. this.bg02.setPosition(-this._bgMovingRange, 0, 0);
  24. }
  25. private _moveBackground(deltaTime: number) {
  26. this.bg01.setPosition(this.bg01.position.x + this._bgSpeed * deltaTime, 0, 0)
  27. this.bg02.setPosition(this.bg02.position.x + this._bgSpeed * deltaTime, 0, 0)
  28. if (this.bg01.position.x > this._bgMovingRange) {
  29. this.bg01.setPosition(this.bg02.position.x - this._bgMovingRange, 0, 0)
  30. } else if (this.bg02.position.x > this._bgMovingRange) {
  31. this.bg02.setPosition(this.bg01.position.x - this._bgMovingRange, 0, 0)
  32. }
  33. }
  34. }