在我的弹跳球程序中,它要求用户插入一些球,这些球将显示在屏幕上,然后从画布顶部开始,然后在画布底部的一条线上弹回。 / p>
当 finish = true 时,动画将停止。到目前为止,一旦第一个球的X位置超过550,动画就会停止。当每个球的X位置超过550时,如何使动画结束呢?
public void multiBounce(int numBalls)
{
BouncingBall[] balls;
balls = new BouncingBall[numBalls];
int x = 50;
int y = 150;
for (int i = 0; i < balls.length; i++){
balls[i] = new BouncingBall(x, y, 16, Color.blue, ground, myCanvas);
x = x + 20;
y = y - 30;
balls[i].draw();
}
boolean finished = false;
while(!finished) {
for (int i = 0; i < balls.length; i++){
balls[i].move();
}
for (int i = 0; i < balls.length; i++){
if (balls[i].getXPosition() >= 550){
finished = true;
}
}
}
答案 0 :(得分:0)
只需检查每个球是否超出您想要的位置。此代码段应该可以完成这项工作。只需用下面的代码替换最后一个for循环。
finished=true;
for (int i = 0; i < balls.length; i++){
if (balls[i].getXPosition() >= 550){
finished = finished && true; }
else{
finished=false;
break;
}
}
答案 1 :(得分:0)
您应该使用break
语句。当它在loop
内遇到时,loop
立即终止,程序控制在loop
之后的下一个语句处恢复。有关详细信息,请参阅Branching Statements
public void multiBounce(int numBalls)
{
BouncingBall[] balls;
balls = new BouncingBall[numBalls];
int x = 50;
int y = 150;
for (int i = 0; i < balls.length; i++){
balls[i] = new BouncingBall(x, y, 16, Color.blue, ground, myCanvas);
x = x + 20;
y = y - 30;
balls[i].draw();
}
boolean finished = false;
while(!finished) {
for (int i = 0; i < balls.length; i++){
balls[i].move();
}
for (int i = 0; i < balls.length; i++){
if (balls[i].getXPosition() < 550){
finished = false;
break;
}
else {
finished = true;
}
}
}
}