简体   繁体   中英

Processing - Make an object disappear and appear in certain frames of time

This is my current code:

int doorCounter = 0;

void setup()
{
 size(512, 348); //width and height of screen
 doorCounter = (int)random(180,300);
}

void draw()
{
 display();
 doorCounter = doorCounter - 1; // Decrease count by 1
 if (doorCounter <= 0) 
  {
   fill(255);
   rect(420, 190, 55, 100); //house door outline
   rect(435, 210, 25, 25, 7); // house door window
   ellipse(435, 255, 8, 8); // house door handle 
   doorCounter = (int)random(180,480); 
  }
}

void display()
{
  fill(255);
  rect(420, 190, 55, 100); //house door outline
  fill(0,0,0); // fill the following polygons in black
  rect(435, 210, 25, 25, 7); // house door window
  ellipse(435, 255, 8, 8); // house door handle
}

However what this code does is just makes the object disappear for a fraction of a second and just makes it reappear instantly. How do I make it so that the object stays disappeared for 3-8 seconds on a random interval just like how the object disappears every 3-8 seconds given that its still on the screen?

Ps I don't know if what I'm trying to achieve makes sense so please feel free to question.

An idea is to use a timestamp and check the time elapsed from it, something like this:

int min_time = 3000; // in ms
int max_time = 8000; // in ms

int time_frame = (int)random(min_time, max_time);

int time_stamp = 0;

boolean show_door = true;

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

void draw()
{
  background(200);

  int time_passed = millis() - time_stamp;

  if (time_passed < time_frame && show_door) {
    display();
  } else if (time_passed >= time_frame) {
    time_stamp = millis();
    time_frame = (int)random(min_time, max_time);
    show_door = !show_door;
  }
}

void display()
{
  fill(255);
  rect(420, 190, 55, 100); //house door outline
  fill(0, 0, 0); // fill the following polygons in black
  rect(435, 210, 25, 25, 7); // house door window
  ellipse(435, 255, 8, 8); // house door handle
}

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