简体   繁体   English

Java游戏计时动作

[英]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. 我将球对象存储在ArrayList中,此刻,我正在这样做。

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. 移动功能仅更改球的y坐标,因此它会向下移动到屏幕上。

At the moment, it is all being painted at exactly the same time and fall at exactly the same time. 此刻,它们都在完全相同的时间绘制并且在完全相同的时间掉落。

eg http://puu.sh/xsGF 例如http://puu.sh/xsGF

How do I make it so they fall at random intervals? 我该如何使它们以随机间隔掉落?

My move() function is as follows. 我的move()函数如下。

    public void move() {

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

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
  }
}

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. 当计时器关闭时(例如,每10毫秒关闭一次),请选择一个随机的球,然后让其下降1像素。 ( 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. 将y坐标增加该速度值,这样,球将全部通过球网以不同的速率掉落。

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);

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. ;)另外,我建议在随机实例处添加球,并且不能与我使用的startFallTime一起使用,因为这在物理上是不正确的。 Depends on your needs though, so you have to figure out the right way by yourself. 不过,这取决于您的需求,因此您必须自己找出正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM