0

私はこのコードを持っています:

boolean run;

void setup() {
  size(1440, 900, P3D);
  background(0);
}

void draw() {

  if (keyPressed && key == 'K') {
    run = true;
  } 

  while (run) {
    stroke(100, 200);
    fill(255, 200);
    float xstart = random(10);
    float ynoise = random(10);

    translate(width/2, height/2, 0);

    for (float y=-(height/8);y<=(height/8);y+=3) {
      ynoise += 0.02;
      float xnoise = xstart;

      for (float x=-(height/8);x<=(height/8);x+=3) {
        xnoise += 0.02;
        drawPoint(x, y, noise(xnoise, ynoise));
      }
    }
    run = false;
  }

  if (keyPressed && key == ENTER) {
    background(0);
  }
}

void drawPoint(float x, float y, float noiseFactor) {
  pushMatrix();
  translate(x*noiseFactor*4, y*noiseFactor*4, -y);
  float edgeSize = noiseFactor*26;
  ellipse(0, 0, edgeSize, edgeSize);
  popMatrix();
}

ただし、「k」を押すと、while ループのコードは実行されません。その理由について何か提案はありますか?

4

2 に答える 2

3

shift + k.... を押すか、コードを次のように変更してみてください。

(keyPressed && (key == 'K' || key == 'k'))

余談ですが...ボタンを1回押して効果を1回トリガーしたい場合は、おそらくvoid keyPressed()を使用したほうがよいでしょう

于 2013-11-08T16:36:02.577 に答える
1

キーを押すnoLoop()と、機能とシーン全体を使用できます。redraw()

void setup() {
  size(1440, 900, P3D);
  background(0);
  noLoop();
}

void draw() {
  stroke(100, 200);
  fill(255, 200);
  float xstart = random(10);
  float ynoise = random(10);

  translate(width/2, height/2, 0);

  for (float y=-(height/8);y<=(height/8);y+=3) {
    ynoise += 0.02;
    float xnoise = xstart;

    for (float x=-(height/8);x<=(height/8);x+=3) {
      xnoise += 0.02;
      drawPoint(x, y, noise(xnoise, ynoise));
    }
  }
}

void drawPoint(float x, float y, float noiseFactor) {
  pushMatrix();
  translate(x*noiseFactor*4, y*noiseFactor*4, -y);
  float edgeSize = noiseFactor*26;
  ellipse(0, 0, edgeSize, edgeSize);
  popMatrix();
}

void keyPressed(){
  if(key == 'K' || key == 'k')
    redraw();

  if(key == ENTER){
    redraw();
    background(0);
  }      
}
于 2013-11-09T19:19:15.120 に答える