I am currently working on a simple game. I have a projectile and an enemy. I want the variable colliding to be 1 whenever the given Sprites touch. Here is my code:
if (projectileposX >= enemyposX + 100 || projectileposX + 160 <= enemyposX) { colliding = 0; }
if (projectileposY >= enemyposY + 100 || projectileposY + 160 <= enemyposY) { colliding = 0; }
else { colliding = 1; }
The "projectile" sprite is 160x160px big.
The "enemy" sprite is 100x100px big.
I wanted to now test for the variable colliding and run code if the value equals 1. Problem is: it acts like colliding is always 1, although I believe that should not be the case.
But because I know its always the problem of the user and I cant see any mistakes I am asking here if anybody can see something wrong.
I also tried combining both if statements into one, resulting in the same weird acting:
if (projectileposY >= enemyposY + 100 || projectileposY + 160 <= enemyposY || projectileposX >= enemyposX + 100 || projectileposX + 160 <= enemyposX) {
colliding = 0;
} else {
colliding = 1;
}
if (colliding == 1) {
//do stuff
}
just to clarify: The if-statement doesnt test if it touches, it test if it's not touching.
I knew I was the problem (again). When declaring the position of the enemy to test it using this code, I declared them wrong. Those variables then stayed undefined and as a result it didnt know what to do with it. Both codes up there are working perfectly fine now without changing a single letter of code. Still thanks to everyone who at least tried to help.