Search code examples
javaprocessing

Running mouseReleased() Only under certain conditions


I'm creating a program where a ball is spawned, and can be thrown by clicking and dragging the mouse. I've decided to add a feature where a wall can be added by clicking a button. The wall would then be moved to the position of the mouse.

void setup() {
  size(640,360);
  background(50);
}

  // Speed of ball
 float xSpeed = 5;
 float ySpeed = 1;
 // Ball size
 float circleRad = 12;
 // Ball position
 float circleX = 0 + circleRad;
 float circleY = 180;
 // Ball physics
 float gravity = 0.35;
 float friction = 0.075;
 // Wall position
 float wallX = 320;
 float wallY = 180;
 // Wall dimensions
 float wallWidth = 20;
 float wallHeight = height;
 // Determines whether or not the wall's position is currently being changed
 boolean wall = false;

void draw() {
  background(50);
  noStroke();  

  fill(85);
  rect(25,25,50,50);

  ellipseMode(RADIUS);
  rectMode(CENTER);
  // If button is pressed, wall is being created/moved
  if(mousePressed && mouseX <= 50 && mouseY <= 50) {
    wall = true;
  }
  // If mouse is pressed and wall isnt being created/moved, move circle to mouse's position
  if (mousePressed && wall == false) {
    circleX = mouseX;
    circleY = mouseY;
  }

  // Creates/moves wall
  if(wall == true){
    wallX = mouseX;
    fill(170);
    rect(wallX, wallY, wallWidth, wallHeight);
  }

  // Creates ball
  fill(255);
  ellipse(circleX,circleY,circleRad,circleRad);

  // Bounces ball off of walls/ceiling/floor, implements ball physics
  if (!mousePressed){
    circleX+=xSpeed;
    circleY+=ySpeed;

    if(circleX >= width - circleRad) {
      xSpeed *= -1;
      circleX = width - circleRad;
    }

    if (circleX <= 0 + circleRad) {
      xSpeed *= -1;
      circleX = 0 + circleRad;
    }

    if (circleY >= height - circleRad) {
      ySpeed *= -1;
      ySpeed += 0.9;
      circleY = height - circleRad;
    }

    if (circleY <= 0 + circleRad) {
      ySpeed *= -1;
      circleY = 0 + circleRad;
    }

    if((circleY <= 0 + circleRad || circleY >= height - circleRad) && xSpeed >= 0) {
      xSpeed -= friction;
    }
    if((circleY <= 0 + circleRad || circleY >= height - circleRad) && xSpeed <= 0){
      xSpeed -= friction * -1;
    }

    if(xSpeed >= -0.1 && xSpeed <= 0.1) {
      xSpeed = 0;
    }

    if(xSpeed <= 0){
      xSpeed -= friction * -0.1;
    }

    if(xSpeed >= 0){
      xSpeed -= friction * 0.1;
    }
    ySpeed += gravity;
    }
}

// Sets ball speed to speed of mouse once mouse is released, allowing for throwing of ball
void mouseReleased(){
   xSpeed = mouseX - pmouseX;
   ySpeed = mouseY - pmouseY;
}

I have not yet completed this feature, (the ball does not yet bounce off of the wall, and there is no way to stop the wall from following the mouse) but I have run into a problem. Whenever I click the button to create the wall, the program sees that I have released the mouse, and decides to throw the ball. Normally someone would not move the mouse when they click, so the result is that the ball's speeds are set to 0. I attempted to resolve this by wrapping the code inside of the mouseReleased() function in an if statement:

void mouseReleased(){
   if(wall == false){
     xSpeed = mouseX - pmouseX;
     ySpeed = mouseY - pmouseY;
  }
}

But this results in another problem; while the mouse is pressed down, the ball pauses in the air, and resumes when the mouse is released. How can I make it so that the button may be pressed, the wall starts to be built, and the ball remains unaffected entirely?


Solution

  • You have all of the logic for moving your ball in this if statement:

    if (!mousePressed){
    

    So, your ball only moves when the mouse is not pressed down. In other words, when you have the mouse pressed down, the ball stops moving.

    The quick and dumb fix would be to add some logic to this if statement. Think about when you want the ball to move: when the mouse is not pressed, or the mouse is pressed but you're moving the wall, right? That would look like this:

    if (!mousePressed || wall){
    

    But more generally, you might want to take a step back and clean your code up a little bit. Look how many times you're checking mousePressed and wall in if statements. You should probably try to refactor that so your logic is a little easier to read. That will help you think about exactly what the code is doing, which will make your life much easier.