float x = 0;
void setup() {
size(200,200);
frameRate(60);
background(255);
}
int v = (int)random(0,width);
int b = (int)random(0,height);
void draw() {
drawCar(v,b,50,0);
secondDelay(5);
v = (int)random(0,width);
b = (int)random(0,height);
}
void drawCar(int x, int y, int thesize, int col) {
int offset = thesize/4;
//draw main car body
rectMode (CENTER);
stroke(200);
fill(col);
rect(x,y,thesize, thesize/2);
//draw wheel relative to car body center
fill(0);
rect(x-offset,y-offset,offset,offset/2);
rect(x+offset,y-offset,offset,offset/2);
rect(x+offset,y+offset,offset,offset/2);
rect(x-offset,y+offset,offset,offset/2);
}
void secondDelay(int aSecond){
int aS;
aS = aSecond * 60;
while(aS > 0) {
aS--;
}
}
what I'm trying to do is make the secondDelay()
function work as a way of delaying the draw
function (which is a loop that updates every frame). also the frameRate itself is 60, so 60 frames per second is what draw()
is running at.
This is done by giving the amount of seconds I want as the argument of secondDelay(amountofseconds)
. then inside that secondDelay()
function
it multiplies the amount of seconds by the amount of frames per second which is 60. Now i have the amount of frames that i need to wait which i put in a variable called aS
. What i do then inside the secondDelay()
function, is make a while loop that runs everytime it sees that variable aS
is bigger than 0. Then i just subtract 1 from aS
everytime it loops until it is no longer bigger than 0. That's when the amount of seconds u wanted it to delay has been delayed and then it does the whole draw()
loop again.
That's how I explain the code that i made, but it doesn't work. Instead it is just acting like secondDelay(5);
doesn't exist so it just creates new cars every frame.
How would i go about fixing this?