Search code examples
javaif-statementbluej

How do I use an if statement to run except when something else has happened?


I am using Java to run balls around a box. By using this piece of code it bounces off the edges.

if (y >= borderBottom)
{
  y = border;
  speedOfY = -speedofY;
}

I have a hole in the box of balls. If the ball hits the hole I would like it to continue through the gap.

I want the if statement to run until:

  • it's between two points on x (the hole)

  • and at the border bottom on the y axis (the side of the hole).

How can I make this happen? I know I need to use simulation.pauseSimulation() but I don't know how to get the balls to stop specifically when it's between the two points and when it's at the border bottom, thanks!

I have tried using the previous statement with this afterwards,

if(y >= borderBottom && (x < 275) && (x > 325>))
  simulation.pauseSimulation()
  

but I have played around and the simulation has ignored the whole bottom border and at one point all the objects flashed.


Solution

  • First of all, you need to know the bottom and top coordinates of the hole. Afterwards, you can simply write:

    if ( y >= borderBottom || ( y < holeBottom && y > holeTop ) ) {
        Simulation.pauseSimulation();
        //You can pause simulation or do anything you prefer
    }