I want to get a condition inside an if running for x seconds

edited September 2016 in Questions about Code
PFont mono;
float x;
float y;
float speed;
int m=millis();

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() {
  speed+=1.5;
  if (260-speed<250) {
    speed*=-1;
  }
  if (mouseX>85 && 95<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("Ê", 90+random(-10, 10), 270-speed);
    fill(255);
    text("X", 190, 270);
    text("T", 295, 270);
    text("A", 385, 270);
    text("S", 480, 270);
    text("E", 580, 270);
  } 
  if (mouseX>185 && 195<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("X", 190+random(-10, 10), 270-speed);
    fill(255);
    text("Ê", 90, 270);
    text("T", 295, 270);
    text("A", 385, 270);
    text("S", 480, 270);
    text("E", 580, 270);
  } 
  if (mouseX>290 && 300<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("T", 295+random(-10, 10), 270-speed);
    fill(255);
    text("Ê", 90, 270);
    text("X", 190, 270);
    text("A", 385, 270);
    text("S", 480, 270);
    text("E", 580, 270);
  }
  if (mouseX>380 && 390<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("A", 385+random(-10, 10), 270-speed);
    fill(255);
    text("S", 480, 270);
    text("E", 580, 270);
    text("Ê", 90, 270);
    text("X", 190, 270);
    text("T", 295, 270);
  }
  if (mouseX>475 && 480<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("S", 480+random(-10, 10), 270-speed);
    fill(255);
    text("E", 580, 270);
    text("Ê", 90, 270);
    text("X", 190, 270);
    text("T", 295, 270);
    text("A", 385, 270);
  }
  if (mouseX>575 && 585<mouseX && mouseY>220 && 320>mouseY) {
    background(0);
    fill(random(255), random(255), random(255));
    text("E", 580+random(-10, 10), 270-speed);
    fill(255);
    text("Ê", 90, 270);
    text("X", 190, 270);
    text("T", 295, 270);
    text("A", 385, 270);
    text("S", 480, 270);
  }
}

This is my code and i want to make it so that when the mouse has left a certain position it keeps doing the cicle for 5 seconds, and that after that it returns to the position that is the setup.

Thanks in advance

Tagged:

Answers

  • Please link between crossposts. This question has already been answered here: http://stackoverflow.com/questions/39745998/i-want-to-get-a-condition-inside-an-if-running-for-x-seconds

  • Answer ✓

    The way you have written the if statement conditions is confusing and can lead to logic errors. For instance in line 25 you have the conditions

    mouseX>85 && 95<mouseX

    Now consider the results for different values of mouseX

    mouseX   |  mouseX > 85  |  95 < mouseX  |  mouseX > 85  &&  95 < mouseX
    80       |      false    |      false    |          false
    90       |      true     |      false    |          false     <<< true??????
    100      |      true     |      true     |          true      <<< false??????
    

    The expression 95 < mouseX is the same as mouseX > 95 so the expression can be written as mouseX > 85 && mouseX > 95. This can ONLY be true when mouseX is greater than 95 so the first condition has no effect on the result.

    I assume that you want the whole expression to be true if mouseX is in the range 85 to 95 and mouseY in the range 220 and 320, if that is the case then line 25 should be

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

    the program logic is much clearer than before so is less likely to have logic errors.

  • I must be blind but I can see the hyperlink that takes me from stackoverflow to happycoding

    Still its nice to be in agreement and repetition is a good teaching strategy. :)

  • @Quark The link from Stack Overflow to Happy Coding (new tutorial site I'm working on) is buried in the comments to my answer on Stack Overflow. The OP had a follow-up question but had run out of Stack Overflow questions and asked where to talk to me, so I sent him to the forum I'd just set up.

Sign In or Register to comment.