aworkinghuman
aworkinghuman

Reputation: 170

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:

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.

Upvotes: 0

Views: 119

Answers (2)

Emmanuel Sarpong
Emmanuel Sarpong

Reputation: 88

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
}

Upvotes: 0

Dec
Dec

Reputation: 303

It may be a good time to learn about logical operators (&&, &, ||, |, !, ^). Specifically in this case:

if (Condition A && Condition B)
{
   //Do something 
}

You could also use:

if(Condition A || Condition B)
{
    //Do something
}

Upvotes: 2

Related Questions