dimanche 4 novembre 2018

Interacting with specific elements in an array; Processing 3

I'm trying to create a grid of squares that "turn on" when the correct key is typed, then "turn off" when they are clicked with a mouse. In my program, a random index number is generated which corresponds to the Unicode value for particular key, and a random square on a grid is colored green when you press that key. Upon the re-coloring, a new index number in generated for a different key, and so on. A mouse click on the last colored square is able to "un-color” it (change to black), but not any other previously colored squares.

The problem seems to be that the mousePressed code is tied to whichever element in the array was last colored, but I can’t figure out how to make it interact with any of the elements in the array that have been colored. Is it even possible? I considered changing the array so that every element's position in the array itself is shuffled, but the shapes that it creates are still arranged in a grid, then iterating backwards with every mouse click. But I can’t seem to figure out how to shuffle an array without getting into java, which I'm not familiar with. Is this a solvable problem or should I somehow adjust my code? Here's what I have so far:

Primary script:

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);
  }
}

"Box" class:

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;
    }
  }
}




Aucun commentaire:

Enregistrer un commentaire