Reputation: 15
say i have 4 variables:
int counter = 0;
int counter2 = -6;
int counter3 = -12;
int counter4 = -18;
These 4 variables increase in a certain if statement that fires from time to time as such:
if (such and such)
{
counter++;
counter2++;
counter3++;
counter4++;
}
and in order these draw circles around the screen in rows, so counter draws circles in the first row, and once that row is filled, counter2 fills the second row and so on:
for (int i = 0; i < counter; i++) {
ellipse(ScoreX+ i * 80, 40, BallSize, BallSize);
}
for (int j = 0; j < counter2; j++)
{
ellipse(ScoreX + j * 80, 120, BallSize, BallSize);
}
for (int k = 0; k < counter3; k++)
{
ellipse(ScoreX + k * 80, 200, BallSize, BallSize);
}
for (int l = 0; l < counter4; l++)
{
ellipse(ScoreX + l * 80, 280, BallSize, BallSize);
}
My problem is that once the circles have been drawn all over the screen, I want this whole process to reset and keep happening until the program has been manually closed by me.
What i mean by this is that, the program will know that counter4 (the last row of circles) has filled up the space with circles and once the last row is filled up, the program resets this whole process going back to filling row one.
Also, is there a way that this loop can be simplified?
EDIT: I found a way to restart it with a simple if statement as such:
if (counter4 == 7)
{
counter = 0;
counter2 = -6;
counter3 = -12;
counter4 = -18;
}
Upvotes: 2
Views: 250
Reputation: 1385
Here is how I would do it :
public class drawCircles implements Runnable{ //To run it in background
private boolean running; //This variable is used to know when thread should be stopped
private int counter = 0; //Here are your counters
private int counter2 = -6;
private int counter3 = -12;
private int counter4 = -18;
private int BallSize;
private int ScoreX;
public drawCircles(int _BallSize, int _ScoreX)
{
running = true;
BallSize = _BallSize;
ScoreX = _ScoreX;
}
private void draw(int counter, int value)
{
for (int i = 0; i < counter; i++)
{
ellipse(ScoreX + i * 80, value, BallSize, BallSize);
}
}
public void run()
{
while (running)
{
draw(counter, 40);
draw(counter2, 120);
draw(counter3, 200);
draw(counter4, 280);
}
}
public void terminate()
{
this.running = false;
}
}
You can call terminate()
when the window is about to get closed, to end the thread
.
I also put your loop in function, to avoid code duplication. Just send the counter you need to it, and also the value that changes (I don't what this is used for, so I called it value
in the code).
In case you need to modify the counters or any other values contained in DrawCircles,
when the if
statements are fired for example, just declare setters
.
Making this class implementing Runnable
is used to make it run in background. The only thing you need to know, now, is how to start it. See here.
It might not exactly fit your needs, as I don't have all information, but I think that's a good start.
Upvotes: 1