【问题标题】:mousePressed within certain region in ProcessingmousePressed 在处理中的某个区域内
【发布时间】:2016-11-25 23:41:50
【问题描述】:

我有一个背景图片,我希望用户认为他们正在通过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-statement arduino mouseevent processing


    【解决方案1】:

    密切关注这一行:

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

    注意它以; 分号结尾。

    这基本上是说“如果鼠标在该区域内,则什么也不做”。然后它会进入下一个代码块并始终运行它,这就是为什么您总是看到 "yikes" 打印出来的原因。

    这一行也有编译错误:

    else println("hello"));
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多