在 Processing 中的特定区域内 mousePressed

mousePressed within certain region in Processing

我有一张背景图片,我希望用户认为他们正在通过 Processing 与之互动。图像上有一个单词列表,当用户点击单词周围的区域时,我希望播放声音并将序列号发送到 Arduino。

除此之外,我没法正确输入 mousePressed 代码。我现在正在使用 println("yikes") 对其进行测试,现在无论我在屏幕上的哪个位置单击,我都会得到 "yikes".

最重要的是,我在其他方面遇到了我无法弄清楚的错误。帮助表示赞赏。

  void setup() {
  size(1475, 995);
  // The image file must be in the data folder of the current sketch 
  // to load successfully
  img = loadImage("PaceTaker.jpg");  // Load the image into the program
}

void draw() {
  // Displays the image at its actual size at point (0,0)
  image(img, 0, 0);
}
void mousePressed() {
  if (mouseX>105 && mouseX<337 && mouseY>696 && mouseY<714); 
  {
    println("yikes");
    stroke(0);
  }
  else println("hello"));
}

密切关注这一行:

if (mouseX>105 && mouseX<337 && mouseY>696 && mouseY<714); 

注意它以 ; 分号结尾。

这基本上是说 "if the mouse is inside the region, do nothing." 然后它到达下一个代码块并始终运行它,这就是为什么您总是看到 "yikes" 打印出来的原因。

你在这一行也有编译错误:

else println("hello"));

因为它有一个额外的)右括号。

要解决这两个问题,请养成始终在 ifelse 语句中使用 { } 大括号的习惯,即使它们只是一行,并且始终检查杂散的 ; 分号:

  if (mouseX>105 && mouseX<337 && mouseY>696 && mouseY<714) {
    println("yikes");
    stroke(0);
  } else {
    println("hello");
  }