Using an if statement within loops? - Processing

2019-03-06 22:58发布

问题:

Lets say I have to use an if statement within a for loop and the for loop fires at a certain condition and the if statement only fires when the for loop has reached a certain stage.

For example, the condition is a counter that counts up when a certain thing happens such as a ball falling down the screen. circles are drawn one by one everytime the ball crosses the screen. When the circles in the first row have reached the end of the screen, the circles start appearing on the second row below the first one. However the second row isnt working for me, which I have implemented with the if statement.

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

The if statement is meant to fire only when the balls on the first row are crossing the width, however the whole game just stops instead of firing that row, what seems to be the issue?

回答1:

First advice: learn the proper Java coding convention, learn how to indent your code, and learn to name your variable.

A slight rewrite on your code should make a readable fix:

int scoreStartX = 52;
int scoreStartY = 40;
int scoreBallSize = 40;
// scorePosX/Y means the position the score-ball should be drawn
scorePosX = scoreStartX;  // scoreStartX/Y = starting position of score balls 
scorePosY = scoreStartY;

for (int i = 0; i < score; i++) { 
    ellipse(scorePosX , scorePosY , scoreBallSize , scoreBallSize);

    // increment the positions, and wrap to next col if over screen width
    scorePosX += scoreBallSize ;


   if( scorePosX  > screenWidth) { // next score ball position is beyond the screen
       scorePosX = scoreStartX;
       scorePosY += scoreBallSize;
   }
}

Further refactoring the code to make use of something like Point to represent coordinate

Point scoreStartPos = new Point(52, 40);
int scoreBallSize = 40;
Point scorePos = new Point(scoreStartPos );

for (int i = 0; i < score; i++) { 
   drawCircle(scorePos, scoreBallSize); // a little helper method makes your code easier to read

    // increment the positions, and wrap to next col if over screen width
    scorePos.translate( +scoreBallSize, 0);


   if( scorePos.getX() > screenWidth) { // next score ball position is beyond the screen
       scorePos.setLocation(scoreStartPoint.getX(),
                            scorePos.getY() + scoreBallSize);
   }
}


回答2:

This statement

i = 0; //reset i to be 0

resets the loop index and is therefore likely to cause an infinite loop.



回答3:

You are setting i to 0 every time ScoreX + i * 80 > width, right? There are no changes to ScoreX or width, meaning that the loop will simply count back up to whatever value of i makes that condition true, putting you in an infinite loop.



回答4:

Hope you have found the answer to your question. I give you a possible answer which involves object oriented programming.

Your question is a little bit above the basic stuff.

So a possible answer:

float BallY = 50; // y value of the ball
float BallX = 260; // x value of the ball
int counter = 0;

score[] scores; // object oriented programming
int n = 50; // number of objects

void setup() {
  size(512, 348); //width and height of screen
  frameRate(600);

  scores = new score[n]; // object oriented
  float myx = 40;
  float myy = 40;

  for (int i = 0; i < n; i++) {
    // here we create our n score objects
    // here n = 50 so 50 objects are created.
    scores[i] = new score(myx, myy);
    // here we increase the x coordinate
    myx += 40;
    // here we check the boundaries and
    // if we go past we reset myx to 40
    // and we go one line down
    if (myx > width) {
      myx = 40;
      myy += 40;
    }
  }
}

void draw() {
  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++;
  }

  // we set the color
  fill(255/1, 255/1, 255/2);
  for (int i = 0; i < counter; i++) {
    if (counter < n) {
      // we draw the object
      scores[i].score_draw();
    }
  }
}

// OBJECT ORIENTED : THE CLASS
class score {

  float myx, myy;

  score(float x, float y) {
    myx = x;
    myy = y;
  }

  void score_draw() {
    ellipse(myx, myy, 40, 40);
  }
}

This works but it gets slower over time. You will have to find out why.

Hope this helps you carry on your learning of processing and programming in general.

PEACE.