【问题标题】:Interacting with specific elements in an array; Processing 3与数组中的特定元素交互;处理 3
【发布时间】:2019-04-08 05:14:20
【问题描述】:

我正在尝试创建一个正方形网格,当键入正确的键时“打开”,然后在用鼠标单击它们时“关闭”。在我的程序中,会生成一个随机索引号,该索引号对应于特定键的 Unicode 值,当您按下该键时,网格上的随机方块会显示为绿色。在重新着色时,为不同的键生成一个新的索引号,依此类推。在最后一个彩色方块上单击鼠标可以“取消”它的颜色(变为黑色),但不能“取消”任何其他以前彩色的方块。

问题似乎是 mousePressed 代码与数组中最后着色的任何元素相关联,但我无法弄清楚如何使其与 any 中的元素交互已着色的数组。甚至可能吗?我考虑过更改数组,使数组本身中的每个元素的位置都被打乱,但它创建的形状仍然排列在网格中,然后随着每次鼠标点击向后迭代。但是我似乎无法弄清楚如何在不进入我不熟悉的 java 的情况下对数组进行洗牌。这是一个可以解决的问题还是我应该以某种方式调整我的代码?到目前为止,这是我所拥有的:

主脚本:

int cols = 16;
int rows = 10;
boolean light = false;

Box[][] boxes = new Box[cols][rows];

int keyIndex = int(random(97, 122));
int randI = (int)random(0, cols);
int randJ = (int)random(0, rows);

void setup() {
  size (800, 600);
  background (0);
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      boxes[i][j] = new Box(i, j);
    }
  }
  println(keyIndex);
}


void draw() { 
  if (light == true) {
    boxes[randI][randJ].rollover(mouseX, mouseY);
    boxes[randI][randJ].displayOn();
  } else {
    boxes[randI][randJ].displayOff();
  }
}

void mousePressed() {
  if (boxes[randI][randJ].onPress(mouseX, mouseY)) {
    println("yes");
    light = false;
  } else {
    println("no");
  }
}

void keyPressed() {
  if (boxes[randI][randJ].keyRight()) {
    light = true;
    randI = (int)random(0, cols);
    randJ = (int)random(0, rows);
    keyIndex = int(random(97, 120));
    println(keyIndex);
  }
}

“盒子”类:

class Box {

  float x, y;
  color c;
  int size = 50;

  Box (int valX, int valY) {
    x = valX * size;
    y = (int) random(0, valY) * size;
  }

  void displayOn() {
    fill(c);
    rect(x, y, size, size);
    c = #b1f64d;
  }

  void displayOff() {
    fill(c);
    rect(x, y, size, size);
    c = #000000;
  }

  void rollover(float mx, float my) {
    if (mx > x && mx < x + size && my > y && my < y + size) {
      c = 126;
    }
  }

  boolean onPress(float mx, float my) {
    if (mx > x && mx < x + size && my > y && my < y + size) {
      return true;
    } else {
      return false;
    }
  }

  boolean keyRight() {
    if (key == keyIndex) {
      return true;
    } else {
      return false;
    }
  }
}

【问题讨论】:

    标签: java arrays multidimensional-array random processing


    【解决方案1】:

    你需要一个Box类的成员变量,而不是单个变量boolean light = false;

    <s>boolean light = false;</s>

    class Box {
    
        boolean light = false;
        float x, y;
        color c;
        int size = 50;
    
        Box (int valX, int valY) {
            x = valX * size;
            y = valY * size;
        }
    
        .....
    }
    

    现在每个盒子对象都可以保存信息,无论它是否“点亮”。

    draw 函数中,您必须绘制所有框,每个框取决于其状态
    boxes[i][j].light 为此使用 2 个嵌套的 for 循环:

    void draw() { 
    
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
    
                if (boxes[i][j].light == true) {
                     boxes[i][j].rollover(mouseX, mouseY);
                     boxes[i][j].displayOn();
                } else {
                     boxes[i][j].displayOff();
                }
            }
        }
    }
    

    mousePressed 中,您必须检查状态为boxes[i][j].light 的所有Box 对象,mouseXmouseY 是否打开:

    void mousePressed() {
        boolean hit = false;
        for (int i = 0; i < cols; i++) {
          for (int j = 0; j < rows; j++) {
    
            if (boxes[i][j].light == true && boxes[i][j].onPress(mouseX, mouseY)) {
                boxes[i][j].light = false;
                hit = true;
            }
          }
        }
        println(hit ? "yes" : "no");
    } 
    

    最后你必须在函数keyPressed中设置成员boxes[randI][randJ].light,而不是变量light,它已经不存在了:

    void keyPressed() {
        if (boxes[randI][randJ].keyRight()) {
            boxes[randI][randJ].light = true;
            randI = (int)random(0, cols);
            randJ = (int)random(0, rows);
            keyIndex = int(random(97, 120));
            println(keyIndex, char(keyIndex));
        }
    }
    

    【讨论】:

    • 天哪,我不知道你可以使用点来访问数据,我以为它只是用于方法。这有很大的不同。非常感谢,这个解释得很好。我想知道为什么你必须为每个函数使用嵌套循环? IE。为什么处理需要(我的理解是)为每个函数重新创建一个数组?或者这不是正在发生的事情?
    • @hawkjones 需要嵌套循环来遍历二维数组中的所有框。请注意,您必须检查每个框的.light 状态,如果鼠标打开,您必须询问每个框.light == true 的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多