【发布时间】:2018-11-04 09:00:48
【问题描述】:
假设我必须在 for 循环中使用 if 语句,并且 for 循环在特定条件下触发,而 if 语句仅在 for 循环达到特定阶段时触发。
例如,条件是一个计数器,当某件事发生时计数,例如球从屏幕上掉下来。每次球越过屏幕时,都会一个一个地绘制圆圈。当第一行中的圆圈到达屏幕末尾时,圆圈开始出现在第一行下方的第二行中。但是第二行不适合我,我已经用 if 语句实现了。
float BallY = 50; // y value of the ball
float BallX = 260; // x value of the ball
float ScoreX = 52;
float ScoreY = 40;
int counter;
void setup()
{
size(512, 348); //width and height of screen
counter = 0;
}
void draw()
{
frameRate(600);
background(255);
fill(0);
ellipse(BallX, BallY, 15, 15); //ball that will fall
BallY++; //ball's y value increases each frame
if (BallY > height) //if ball's y value is greater than the screen
{
BallY = 0; //reset the y value of the ball back to 0
counter++;
}
for (int i = 0; i < counter; i++) {
ellipse(ScoreX + i * 80, 40, 40, 40); // draw circles in the first row one by one
if( ScoreX + i * 80 > width) // if the circles cross the width
{
i = 0; //reset i to be 0
ellipse(ScoreX + i * 80, 80, 40, 40); // draw circles in the second row
}
}}
if 语句仅在第一行的球越过宽度时触发,但是整个游戏只是停止而不是触发该行,这似乎是什么问题?
【问题讨论】:
-
请不要破坏你的帖子——现在完全没有意义了。
-
你有没有想过这个问题?如果是,请将答案标记为已接受。
标签: java loops if-statement conditional processing