处理语言中的 void draw 函数



是否可以在if()函数中编写void draw()函数?我尝试编写一个程序,如果单击鼠标,draw()应该为我绘制输出。处理显示指向if()的错误
任何帮助不胜感激,谢谢!

你不能在

抽奖之外运行if(),所以长回答简短:不。

如果你想在

按下鼠标时画一些东西,你有无限的选择。我建议查看您应该覆盖的mousePressed()mouseReleased()函数:

void setup() {
  size(500,500);
}
void draw() {
 // nothing is being drawn here, we'll draw from
 // mousePressed at the end of every frame
}
void mousePressed() {
  rectMode(CENTER);
  rect(width/2,width/2,100,100);
}

您可以使用mousePressed()让其他代码片段知道它们应该执行某些操作,并让它们为draw()中的下一帧做好准备。从中吸取教训通常不是一个好主意。

还有mousePressed变量,它等于true直到释放鼠标:

void setup() {
  size(500,500);
}
void draw() {
  background(0);
 if (mousePressed) {
   rectMode(CENTER);
   rect(width/2,width/2,100,100);
   // if the mouse is released, this code
   // won't be executed, so the background
   // is the only thing that's going to be
   // drawn in that frame
 }
}

最新更新