how to make an infinite movement cycle?

58 Views Asked by At

I'm trying to make a game object to move infinitely in its track by changing its direction. However, it never works by using if-statement. It is also hard to apply since the game object is consistantly updating.

I want to make the object go down until its position is y = 80. Once it is over y=80, it goes back to y=0 until its y position is less or equal to y=0. and so it goes down till y = 80 again...

@override
  void update(double dt) {
    super.update(dt);
    print(hitbox.position.y);

    int hitboxDir = 1;
    if (hitbox.position.y <= 80 && hitboxDir == 1) {
      hitbox.position.y += 10 * dt;
    } else if (hitbox.position.y >= 80) {
      hitboxDir = -1;
      hitbox.position.y += hitboxDir * 10 * dt;
    } else if (hitbox.position.y <= 80 && hitboxDir == -1) {
      hitboxDir = 1;
    }
  }
0

There are 0 best solutions below