jeudi 15 décembre 2016

Multiple If Statements - Checking Player Collision Only Once

I am currently making a game and for player movement, I have if statements that check if you are running into the walls.

Here is a small example of what I am doing:

if(!(player.intersects(wall1)){

        // move

}

However, my problem is that now that I am adding more walls for different levels, the if statements are getting a bit wonky. For example, adding a second wall:

    if(!(player.intersects(wall1)){

            // move

    }
    if(!(player.intersects(wall2)){

            // move

    }

But this just doubles the movement speed since both result in true if you aren't hitting any walls.

What I've Tried

I've tried adding else to it, like this:

if(!(player.intersects(wall1)){

        // move

} else
if(!(player.intersects(wall2)){

        // move

}

But this will result in not checking for both walls.

How can I accomplish adding multiple if-statements that check for multiple walls effectively?

Aucun commentaire:

Enregistrer un commentaire