[Processing] Code problem


#1
PFont mono;
float x;
float y;

boolean treme1 =false;
boolean treme2 =false;
boolean treme3 =false;
boolean treme4 =false;
boolean treme5 =false;
boolean treme6 =false;
int startTime1;
int startTime2;
int startTime3;
int startTime4;
int startTime5;
int startTime6;

void setup() {
  size(720, 480);
  background(0);
  mono = createFont("Helvetica", 100);
  textFont(mono);
  text("Ê", 90, 270);
  text("X", 190, 270);
  text("T", 295, 270);
  text("A", 385, 270);
  text("S", 480, 270);
  text("E", 580, 270);
}

void draw() {
  if (millis()>startTime1+2000) {
    treme1=false;
    startTime1=0;
  }
  if (millis()>startTime2+2000) {
    treme2=false;
    startTime2=0;
  }
  if (millis()>startTime3+2000) {
    treme3=false;
    startTime3=0;
  }
  if (millis()>startTime4+2000) {
    treme4=false;
    startTime4=0;
  }
  if (millis()>startTime5+2000) {
    treme5=false;
    startTime5=0;
  }
  if (millis()>startTime6+2000) {
    treme6=false;
    startTime6=0;
  }
  if (mouseX>85 && 95<mouseX && mouseY>220 && 320>mouseY) {
    startTime1=millis();
    treme1=true;
  }
  if (treme1) {
    fill(0);
    rect(85, 160, 80, 150);
    fill(random(255), random(255), random(255));
    text("Ê", 90+random(-10, 10), 270+random(-10, 10));
  }
  if (mouseX>185 && 195<mouseX && mouseY>220 && 320>mouseY) {
    startTime2=millis();
    treme2=true;
  }
  if (treme2) {
    fill(0);
    rect(180, 160, 85, 150);
    fill(random(255), random(255), random(255));
    text("X", 190+random(-10, 10), 270+random(-10, 10));
  }
  if (mouseX>290 && 300<mouseX && mouseY>220 && 320>mouseY) {
    startTime3=millis();
    treme3=true;
  }
  if (treme3) {
    fill(0);
    rect(280, 140, 85, 150);
    fill(random(255), random(255), random(255));
    text("T", 295+random(-10, 10), 270+random(-10, 10));
  }
  if (mouseX>380 && 390<mouseX && mouseY>220 && 320>mouseY) {
    startTime4=millis();
    treme4=true;
  }
  if (treme4) {
    fill(0);
    rect(370, 140, 90, 150);
    fill(random(255), random(255), random(255));
    text("A", 385+random(-10, 10), 270+random(-10, 10));
  }
  if (mouseX>475 && 480<mouseX && mouseY>220 && 320>mouseY) {
    startTime5=millis();
    treme5=true;
  }
  if (treme5) {
    fill(0);
    rect(475, 140, 90, 150);
    fill(random(255), random(255), random(255));
    text("S", 480+random(-10, 10), 270+random(-10, 10));
  }
  if (mouseX>575 && 585<mouseX && mouseY>220 && 320>mouseY) {
    startTime6=millis();
    treme6=true;
  }
  if (treme6) {
    fill(0);
    rect(575, 140, 90, 150);
    fill(random(255), random(255), random(255));
    text("E", 580+random(-10, 10), 270+random(-10, 10));
  }
}

I can’t seem to pass over the letter “T” for example without the letters before start shaking as well…


#2

It should be noted that this is a continuation of this Stack Overflow question.

Take a look at your if statement:

if (mouseX>85 && 95<mouseX && mouseY>220 && 320>mouseY) {

Pay close attention to exactly what you’re checking here. Taking them one at a time:

mouseX>85

Here you’re checking whether mouseX is greater than 85. Makes sense so far.

95<mouseX

Here you’re checking whether 95 is less than mouseX. Put another way: you’re checking whether mouseX is greater than (or equal to) 95. Why would you check whether mouseX is greater than both 85 and 95?

It looks like what you really wanted to check was whether mouseX is between 85 and 95. In other words, you want to check whether mouseX is greater than 85 and less than 95.

So, you’d want to rewrite the first half of your if statement to look like this:

mouseX > 85 && mouseX < 95

Notice that I’ve kept mouseX on the same side of each inequality, that way it’s easier to see exactly what it’s checking.

You’re going to have to fix the same thing with the mouseY check, and you’re going to have to do this in all of your if statements that check the position.

When I do that, your code works reasonably well. Some of your coordinates are a little off, but you can check that by drawing boxes around the bounds you’re checking to make sure they overlap correctly.

This is also a great reason to use arrays and for loops to clean up your code, but I’ll leave that for a separate discussion.