简体   繁体   中英

how do I make a custom delay function with java in processing 3.4?

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?

For what you want to do, you have to count the frames. It the number of frames exceeds a number, which is equal the delay time span, then you have to draw a new car.
You don't need a function that delays, waits or sleeps for a while, but you need a function that tells and signals you that a period of time has passed.

Create a global frame counter ( frame_count ) and count the frames in the secondDelay function. If the counter exceeds 60*aSecond then the function reset the counter to 0. If the counter is 0, then this is signaled by returning the boolean value true.

int frame_count = 0; 
boolean secondDelay(int aSecond){

    boolean signal = frame_count==0;

    if ( frame_count >= 60*aSecond ) {
        frame_count = 0;
    } else { 
        frame_count ++;
    }
    return signal;
}

Every time when the secondDelay returns true and so gives you the signal, then you have to draw a new car in the draw function:

void draw() {

    if ( secondDelay(5) ) {
        drawCar(v,b,50,0);
        v = (int)random(0,width);
        b = (int)random(0,height);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM