处理中的背景不变

时间:2014-05-12 15:28:26

标签: processing

我正在尝试做一个非常简单的任务,如果用户点击屏幕,那么背景颜色应该在每200毫秒后随机改变10次。

这是我的代码:

void setup()
{
 size(400,400);
}

void draw()
{

}

void mousePressed()
{
  for(int i = 0; i < 10; i++)
  {
      int startTime = millis();
      background(int(random(255)), int(random(255)), int(random(255)));
      while(millis() - startTime < 200){
      }
  }
}

但是,上面的代码只改变背景颜色一次而不是10次。我无法理解我哪里出错了。

1 个答案:

答案 0 :(得分:2)

看看这篇文章...... http://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F 处理中的渲染仅在每个绘制周期结束时完成,因此您只能看到最后一种颜色...我会尝试类似:

// to avoid starting at program's start...
boolean neverClicked = true;

// global
int startTime = 0;

// a counter
int times = 0;

void setup() {
  size(400, 400);
}

void draw() {

  // if clicked and passed half sec and 10 series not complete...
  if ( !neverClicked && millis() - startTime > 500 && times < 9) { 

    // change bg
    background(int(random(255)), int(random(255)), int(random(255)));

    // reset clock
    startTime = millis();

    // count how many times
    times++;
  }
}



void mousePressed() { 

  neverClicked = false;

  // reset timer and counter
  startTime = millis();
  times = 0;

  // do first change immediately
  background(int(random(255)), int(random(255)), int(random(255)));
}