简体   繁体   English

处理-使对象消失并在特定时间内显示

[英]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? 如何使对象保持随机间隔消失3-8秒,就像对象仍然在屏幕上时每3-8秒消失一样?

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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