Grid Boxes Will Not Refresh Properly

edited December 2014 in Questions about Code

Hey everyone,

I am fairly new to coding, and am trying to create a 9x9 3D grid that is capable of being manipulated through the keyPressed() function. I have set it up where keys q,w,e,a,s,d,z,x, and c correspond with the grid, and each key works well individually. The issue is that when I press a second key, after having pressed a first key, it seems to drop that first cell back to zero, while leaving an odd background image.

My code is below, and if anyone has any suggestions, I am eager to hear them:

int Q = 0;    //Row One Left Box int
int W = 0;    //Row One Central Box int
int E = 0;    //Row One Right Box int
int A = 0;    //Row Two Left Box int
int S = 0;    //Row Two Central Box int
int D = 0;    //Row Two Right Box int
int Z = 0;    //Row Three Left Box int
int X = 0;    //Row Three Central Box int
int C = 0;    //Row Three Right Box int
int CLEAR  = 0;    //Clear the grid

void setup() {
  smooth();
  size(600,600,P3D);
  background(0);
}



//CREATE CELLS
void draw() {
  translate(width/2,height/2);
  rotateX(PI/3);
  //create multiple cells

  //Row One Left Box
  pushMatrix();
  fill(100+Q, 20, 20);
  translate(-100,-100);
  box(100,100, Q);
  popMatrix();

  //Row One Central Box
  pushMatrix();
  fill(100+W, 20, 20);
  translate(0,-100);
  box(100,100, W);
  popMatrix();

  //Row One Right Box
  pushMatrix();
  fill(100+E, 20, 20);
  translate(100,-100);
  box(100,100, E);
  popMatrix();

   //Row Two Left Box
  pushMatrix();
  fill(100+A, 20, 20);
  translate(-100,0);
  box(100,100, A);
  popMatrix();

  //Row Two Central Box
  fill(100+S, 20, 20);
  box(100,100, S);

  //Row Two Right Box
  pushMatrix();
  fill(100+D, 20, 20);
  translate(100,0);
  box(100,100, D);
  popMatrix();

   //Row Three Left Box
  pushMatrix();
  fill(100+Z, 20, 20);
  translate(-100,100);
  box(100,100, Z);
  popMatrix();

   //Row Three Central Box
  pushMatrix();
  fill(100+X, 20, 20);
  translate(0,100);
  box(100,100, X);
  popMatrix();

   //Row Two Right Box
  pushMatrix();
  fill(100+C, 20, 20);
  translate(100,100);
  box(100,100, C);
  popMatrix();

  strokeWeight(1);
  stroke(255);
  fill(100);
}



//MAKE CELLS INTERACTIVE

//press corresponding keys from q-c to change height of various cells
void keyPressed() {
  if ((key == 'q')) {
    Q = Q+20;
  } else {
    Q = 0;
  }
   if ((key == 'w')) {
    W = W+20;
  } else {
    W = 0;
  }
   if ((key == 'e')) {
    E = E+20;
  } else {
    E = 0;
  }
   if ((key == 'a')) {
    A = A+20;
  } else {
    A = 0;
  }
  if ((key == 's')) {
    S = S+20;
  } else {
    S = 0;
  }
   if ((key == 'd')) {
    D = D+20;
  } else {
    D = 0;
  }
   if ((key == 'z')) {
    Z = Z+20;
  } else {
    Z = 0;
  }
   if ((key == 'x')) {
    X = X+20;
  } else {
    X = 0;
  }
   if ((key == 'c')) {
    C = C+20;
  } else {
    C = 0;
  }
  //press the '-' key to clear the board
  if ((key == '-')) {
    box(100,100,S);
    background(0);
  } else {
    CLEAR =0;
  }
}

Thanks

Answers

  • Answer ✓

    in keyPressed in every else you set it back to 0

    don't

  • Fantastic! Thank you so much. Definitely overcomplicated something simple. Another question, if you are willing to help. Do you know how I can get the boxes to move in just the vertical direction as opposed to in both directions?

  • you should have background() at the beginning of draw()

    also, pls look into arrays - a list of your cubes

  • concerning movement:

    do you mean translate ? Or what do you mean?

    translate can take 3 params x,y,z each can be - and +

    just pass every thing you need to translate...

    the box command just specifys the size in three directions

  • I am more concerned with whenever a press key q, for instance, the box grows 20, but ten in both the positive and negative directions. I am hoping to be able to control this grid so that I can send the box movement in the positive or negative direction, not both at the same time.

  • Also, thanks for the help. I am very appreciative.

  • ah, I see what you mean.

    it's right

    the pos is always the center

    when you increase size() it goes both sides.

    right....

    to compensate : change the translate for this box as well

    by - Q/2 e.g.

  • Answer ✓

    translate(100,100-Q/2);

    or so

  • Perfect! This is exactly what I was looking for. Thank you so much!

Sign In or Register to comment.