【问题标题】:Processing - Make an object disappear and appear in certain frames of time处理 - 使对象消失并出现在特定的时间范围内
【发布时间】:2018-04-04 21:18:04
【问题描述】:

这是我当前的代码:

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 秒消失一次一样?

P.s 我不知道我想要实现的目标是否有意义,所以请随时提问。

【问题讨论】:

  • 我认为您的代码不完整。您使用的是 Swing 或 JavaFX 或 AWT 还是其他?我建议发布一个 SSCCE。
  • @Abra 代码确实在处理软件上运行
  • 对不起,我不知道 Processing 是一种语言。那么为什么 java 标签是相关的呢?
  • @Abra Processing 不是一种语言,它是一个使用 java 语言的软件

标签: java random time processing


【解决方案1】:

一个想法是使用时间戳并检查过去的时间,如下所示:

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
}

【讨论】:

  • 否,因为它会立即消失。你想要的是添加另一个 if 语句并在那里绘制另一个对象:else if (time_passed &lt; time_frame &amp;&amp; show_door == false)
  • Alberto 你能解释一下“int time_passed = millis() - time_stamp;”是什么吗?代码呢?
  • 它测量自上次 time_stamp 以来经过的时间。 millis() 测量自程序启动以来经过的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 2019-09-18
相关资源
最近更新 更多