Search code examples
javaif-statementcollisionoperation

Java- if else... if something is something or something?


Okay, I'm making this new game in Java. This might be a simple question, but can anyone please help me with this?

So, if the "Guy" collides with the platforms, he can't move right (obviously).

This is what I got:

if(Guy.x + Guy.width == (platform2.x ^ platform3.x)) {} else{
                    goRight();
}

The "^" is supposed to be "or". I have a void called goRight();

so it would be like "if Guy's x plus Guy's width is the same as platform2 or platform3's x then go right. I don't want to have to do this:

if(Guy.x + Guy.width == platform2.x || Guy.x + Guy.width == platform3.x)) {} else{
            goRight();
}

And plus, I have like 20 more platforms so it would be easier the first way if it's possible. And I have to make the left collision detection too.


Solution

  • If Platform is a class that has an x value, then I'd suggest putting put all of your platforms into a collection of some kind and iterating over it. Inside a loop, do something like

    for(Platform p: platforms){
         if(Guy.getX() + Guy.getWidth() == p.getX()){
            //whatever
         }else{
            goRight()
         }
    }