首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理--使物体消失并在一定的时间范围内出现。

处理--使物体消失并在一定的时间范围内出现。
EN

Stack Overflow用户
提问于 2018-04-04 05:57:11
回答 1查看 2.7K关注 0票数 0

这是我目前的代码:

代码语言:javascript
复制
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
}

然而,这段代码所做的只是使对象消失了一秒,并使它立即重新出现。如何使物体在随机间隔内消失3-8秒,就像物体在屏幕上仍然存在的情况下,每3-8秒消失一次?

我不知道我想要达到的目标是否有意义,所以请随意提问。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-04 10:17:06

一种方法是使用时间戳并检查从时间戳中经过的时间,如下所示:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49643797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档