Why are if AND else being executed at the same time?


#1

Hi!
I’m working on a collision system at the moment and everything seemed to work fine (actually not but I want to solve all the other problems on my own lol :sweat_smile:) but then I realised that a boolean in the code was first set to true - in an if-statement - and then to false - in the associated else statement. WHAT? It’s kinda difficult to show the problem without many lines of code, but I’ll try my best here:

if(player.x + player.acceleration[2] > x[0] - playerSizeX &&
           player.x - player.acceleration[1] < x[1] + playerSizeX &&
           player.y + player.acceleration[0] >= y[0] - playerSizeY &&
           player.y < y[3] - player.valueForMovingDown[1])
           {
             player.y = y[0] - playerSizeY;
             player.collision[0] = true;
             println("This should not");
           }else{
             player.collision[0] = false;
             println("happen :/");
           }

It prints out the whole sentence. As I just showed the segment I’m talking about and didn’t explain anything else, feel free to ask for more code!


#4

I figured it out. There are two “Collision”-object in my ArrayList, and since the player didn’t collide with the second one in the list, that one turned the collision off.


#5

Glad you figured it out!!

This is a good example of why it is important to provide a minimal, complete, verifiable example – break your problem down into a simpler sketch that actually runs.

In this case, your problem was an outer loop. However, your code fragment didn’t tell us there was an outer loop – so there would have been no way for the forum to investigate your problem or give specific advice on how to fix it. MCVEs take a bit longer to make, but they protect you and the forum from wasting time hunting in the wrong place.


#6

I just really didn’t want to post like a hundred lines of code, but I’ll try to think about what could be important more next time! :smile: