user432584920684
user432584920684

Reputation: 389

Java game timing movements

I'm trying to make balls fall from the top of the window. I store ball objects in an ArrayList and, at the moment, I am doing this.

for (int i = 0; i < balls.size(); i++) {
    Ball b = (Ball) balls.get(i);
    if (b.isVisible()) {
        b.move();
    }

the move function just changes the y co-ordinate of the ball so it drops down the screen.

At the moment, it is all being painted at exactly the same time and fall at exactly the same time.

e.g. http://puu.sh/xsGF

How do I make it so they fall at random intervals?

My move() function is as follows.

    public void move() {

    if (y > 480) {
        this.setVisible(false);
        System.out.println("GONE");
    }
    y += 1;
}

Upvotes: 1

Views: 375

Answers (4)

brimborium
brimborium

Reputation: 9512

Ok, seeing your move function, this is not really physically correct. You should have a acceleration. This makes the ball fall more realistically (of course there is air resistance etc, but I think this is enough for now). In order the let them fall at random times, you could either add them at random times (make them existing/visible at random time instances) or so.

class Ball {
  private double acc = 9.81; // or some other constant, depending on the framerate
  private double velocity = 0;
  private double startFallTime = Math.random()*100; // set from outside, not here!

  public void move() {
    // check if ball is already here
    if (startFallTime-- > 0) return;
    if (y > 480) {
      this.setVisible(false);
      System.out.println("GONE");
    }
    velocity += acc; 
    y += velocity;
  }
}

EDIT: Of course the acceleration stuff is optional, depending on what you want. If you want linear movement, then your approach is fine, it just looks better if the ball has an acceleration. ;) Also, I recommend adding the balls at random instances and not work with this startFallTime that I used, because this is physically not really correct. Depends on your needs though, so you have to figure out the right way by yourself.

Upvotes: 0

Jack
Jack

Reputation: 133609

The simplest approach, if you want constant velocity, is to place them im random positions putside the top of your viewport.

Since I guess you already draw them outside the screen just add a random displacement there and you are done. eg:

ball.y = -radius + random.nextInt(100);

Upvotes: 0

weston
weston

Reputation: 54801

You could add balls randomly during the game loop.

//add new balls randomly here:
if(<randomtest>) {
    balls.add(new Ball());
}
for (int i = 0; i < balls.size(); i++) { 
  Ball b = (Ball) balls.get(i); 
  if (b.isVisible()) { 
      b.move(); 
  }
  else {
    //also might be good idea to tidy any invisible balls here
    //if you do this make sure you reverse the for loop
  }
}

Upvotes: 1

Hidde
Hidde

Reputation: 11941

There are 2 things you can do:

  1. Add a Timer. When the Timer goes off (every 10 ms for example), select a random ball, and let that one drop 1px. (Mind, you will get balls that will fall at different speeds at different times, because of the random factor)

  2. Use a random value for the speed when initializing the ball. Increase the y coordinate by that speed value, so the balls will all fall at a different rate through the sceen.

Upvotes: 0

Related Questions