Hi @mews,
Welcome to the forum!
Let’s suppose you have the following Processing program:
void drawMissiles() {
// Loops infinitely
while (true) {}
}
void setup() {
size(500, 500);
}
void draw() {
drawMissiles();
// This is never reached
circle(50, 50, 10);
}
The execution steps are:
-
The
setup()
function gets called at the beginning -
At the first draw loop, the
drawMissiles()
function gets called and does awhile
loop that never finish (true is always true). -
The
circle
function is never called since the draw() function is waiting for thedrawMissiles
function to finish.
This is to explain that if you call any function inside the draw()
loop, it will wait until the function returns a value or finishes.
So in your code, you are increasing the position and drawing a point in the while loop but you see it flash on the screen because it does the while loop at each draw loop which is not what you want.
The draw loop is actually the main loop of your program so you need to use it in order to animate objects at each frame (since it’s called roughly 60 times per second). The update code is then called once every loop.
Check this other program:
PVector ballLocation;
void setup() {
size(500, 500);
ballLocation = new PVector(0, height / 2);
}
void draw() {
background(255);
// Move the ball
ballLocation.x += 5;
// Make it wrap when it goes out of screen
if (ballLocation.x > width) ballLocation.x = 0;
// Draw the ball
fill(255, 0, 0);
circle(ballLocation.x, ballLocation.y, 100);
}
So rather than making a while loop where I update the location of my ball, the draw loop does the job for me
One more thing is that you should use Object Oriented Programming which makes program structure more easy to work with. You can check an old post I wrote on this subject: