如何在处理中使用带有鼠标按钮的数组

问题描述 投票:0回答:1

我是编程新手,参加了处理入门课程,对于我的一个学校项目,我决定制作一个“游戏”,用户使用左右按钮“去那个方向”。我想让它好像用户同时单击左键和鼠标按钮超过 10 次,程序将跳出循环并退出游戏。

我尝试使用与 for 循环相同的格式,但用按钮控件替换了 int 值,但我一直收到丢失的操作员消息。这是我的代码:

void draw(){ photo = loadImage("https://cdn1.epicgames.com/ue/item/mackenzie-shirk-highresscreenshot00005-1920x1080-3eed278c8730ae28130aca96322a23d1.jpg"); image(photo, 100, 100); print(f); if (mousePressed && (mouseButton == LEFT)) { //if left button is clicked, you text tells you that you go left println(a); }  else if (mousePressed && (mouseButton == RIGHT)) { //if right button is clicked, text tells you that you go right println(b); } for (mouseButton == LEFT \> 10){ //if string a is print more than 10 times, break break; } for (mouseButton == RIGHT \> 10){ //if string b is print more than 10 times, break break; } }

arrays processing
1个回答
0
投票

您发布的代码有几个问题,您需要处理几个方面。

  1. 发布到 Stack Overflow 时,请通过将源代码放在代码块 ({}) 内或两组重音符之间来格式化您的源代码, ``` 代码在这里 ```

  2. 图像应该在 setup() 中加载。大多数处理应用程序都有 setup() 和 draw() 函数。

  3. 图像确实加载,但除非您使用更大的窗口大小,否则不会显示。

  4. 为了跟踪右击和左击的次数,我建议使用两个全局整数变量:一个用于右击,一个用于左击。

  5. 一次只捕获一次点击,最好使用 mousePressed() 并将左右点击捕获在其中。

  6. '休息;'仅适用于“for”循环以及“switch”和“while”结构。如果你想在超过一定数量的点击时离开应用程序, exit() 会更好。

  7. Processing 使用 println() 而不是 print() 作为控制台日志。

  8. 你的帖子标题提到了一个数组,但你的代码中没有数组。

继续阅读和编写代码,祝你的项目好运。

以下源代码应该可以无误地运行并演示上述要点:

PImage photo;
int leftCounter = 0;
int rightCounter = 0;

void setup() {
  size(800, 600);
  photo = loadImage("https://cdn1.epicgames.com/ue/item/mackenzie-shirk-highresscreenshot00005-1920x1080-3eed278c8730ae28130aca96322a23d1.jpg");
}

void draw() {
  image(photo, 0, 0, photo.width/2, photo.height/2);
}

void mousePressed() {
  if (mouseButton == LEFT) {
    println("left mouse clicked", leftCounter);
    leftCounter++;
    if (leftCounter > 10) exit();
  }
  if (mouseButton == RIGHT) {
    println("right mouse clicked", rightCounter);
    rightCounter++;
    if (rightCounter > 10) exit();
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.