簡體   English   中英

處理-使對象消失並在特定時間內顯示

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

這是我當前的代碼:

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

附:我不知道我要實現的目標是否有意義,請隨時提出疑問。

一個想法是使用時間戳記,並檢查它經過的時間,如下所示:

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