keyDown() 不工作处理

keyDown() Not working Processing

我正在使用 Processing 制作一些东西,但基本上我的 keyDown() 无法正常工作。它应该在按下任何键但未调用该功能时触发。代码如下:

int playerno=0; //determines player
boolean ready=true;
void setup() {
  size(700, 700);
  background(#FFFFFF);
  fill(#000000);
  textSize(50);
  text("Press Any Key To Start", 350, 350);
}

void keyPressed() {
  if (ready) {
    fill(#FFFFFF);
    rect(350, 350, 200, 100);
    fill(#000000);
    textSize(50);
    text("Game Ready", 350, 350);
    boolean ready=false;
  }
}

如果没有 draw 功能,这将无法工作。此外,您在 keypressed() 内声明了新的局部变量 ready 这是一个严重的错误。尝试将绘图代码从 "keyDown()" 移动到 "drawing",如下所示:

void draw() {
  if (ready == false) {
    background(#FFFFFF);     //This is needed for redrawing whole scene
    fill(#FFFFFF);
    rect(350, 350, 200, 100);
    fill(#000000);
    textSize(50);
    text("Game Ready", 350, 350);
  }
}

void keyPressed() {
  if (ready) {
    ready=false;
  }
}