1
votes
  void draw() {

  boolean onTheMoon = false;
  drawGrid(); // Old overlay animation. 
  if ((mouseX > 50) && (mouseX < 150) && (mouseY > 50) && (mouseY < 150)) {
    onTheMoon = true;
  } 
  if (onTheMoon) 
  {
    do 
    {
      moonLanding(); // New overlay method 
    } 
    while ((mouseX > 800) && (mouseX < 1000) && (mouseY > 400) && (mouseY < 700)); //Coordinates for new location on overlay
    }
  rocketShip(); // Calling my rocketship method. Rocket made of shapes using mouseX and mouseY
}

Hi everyone, I am trying to keep an overlay running once the mouseX and mouseY coordinates match the conditions of the top if statement in the code above. The problem I am facing now is when the mouseX and mouseY move away from the area the old overlay appears and it is ruining the animation. I am testing out different methods but nothing is working. Does anyone have any suggestions?

1
The do/while part looks suspicious. Remember you're calling this in draw() so rocketShip() won't be called (and rendering can't continue) until the while loop completes execution. What should happen when the mouse is within 50,150 bounding box ?George Profenza
Hi George, When the mouse hits the 50,150 boundary a black background appears with a circle shape in the lower right. I want the overlay to stay present until the mouseX and mouseY coordinates reach the new condition (which is the circle shape in the lower right)KokaDoodles
If I were to remove the do/while what other technique could I use?KokaDoodles

1 Answers

3
votes

Based on your comments above it sounds like an if condition will do the job and there's no need for the do/while loop.

You have this boolean expression: (mouseX > 800) && (mouseX < 1000) && (mouseY > 400) && (mouseY < 700).

You want moonLanding() to be called only when this condition is not(!) met:

 void draw() {

  boolean onTheMoon = false;
  drawGrid(); // Old overlay animation. 
  if ((mouseX > 50) && (mouseX < 150) && (mouseY > 50) && (mouseY < 150)) {
    onTheMoon = true;
  } 
  if (onTheMoon) 
  {
    if (!((mouseX > 800) && (mouseX < 1000) && (mouseY > 400) && (mouseY < 700))){
      moonLanding(); // New overlay method 
    }
  rocketShip(); // Calling my rocketship method. Rocket made of shapes using mouseX and mouseY
}

this is equivalent to:

 if (((mouseX > 800) && (mouseX < 1000) && (mouseY > 400) && (mouseY < 700)) == false){
          moonLanding(); // New overlay method 
        }

One suggestion I have, if your program will require more mouse/bounding conditions is to encapsulate the conditions in a re-usable function.

for example:

 void draw() {

  boolean onTheMoon = false;
  drawGrid(); // Old overlay animation. 
  if (isMouseOverTopLeft()) {
    onTheMoon = true;
  } 
  if (onTheMoon) 
  {
    if (!isMouseOverBottomRight()){
      moonLanding(); // New overlay method 
    }
  rocketShip(); // Calling my rocketship method. Rocket made of shapes using mouseX and mouseY
}

boolean isMouseOverTopLeft(){
  return isMouseOverRect(50, 50, 150, 150);
}

boolean isMouseOverBottomRight(){
  return isMouseOverRect(800, 400, 1000, 700);
}

boolean isMouseOverRect(float x1, float y1, float x2, float y2){
  return (mouseX >= x1 && mouseX <= x2) && (mouseY >= y1 && mouseY <= y2);
}

I can easily see getting ridiculous with many isOverX methods, but by then you could start storing making a data structure to hold bounds an a list of bounds, etc. The idea is to make the code easier to read.