I’m attempting to make a little if statement that decides whether to render a sprite to the screen or not. On paper it makes perfect sense but for some reason it’s not working at all when I put it into code.
I’ve managed to get one of the equations to work, xLocation > (x+400), everything else just doesn’t work no matter how I’ve tried it.
Code Snip:
// Decides whether to render the sprite or not.
int x = player.getXLocation();
int y = player.getYLocation();
if(xLocation > (x+400) || xLocation < (x-400) || yLocation < (y-400) || yLocation > (y+400))
{
renderMe = false;
}
else
{
renderMe = true;
}
What I’m trying to do is check if the X and Y locations of the enemy sprite are within 400 in any cardinal direction of the player and if it is then render it, if not then don’t render it. It’s kind of like creating a box around the player and checking if the enemy is inside of it. The four equations I’ve made up are:
- enemyYPosition < playerYPosition-400
- enemyYPosition > playerYPosition+400
- enemyXPosition < playerXPosition-400
- enemyXPosition > playerXPosition+400
They stand for:
- If the enemy is further than 400 South of the player.
- If the enemy is further than 400 North of the player.
- If the enemy is further than 400 West of the player.
- If the enemy is further than 400 East of the player
I probably just messed up my math somehow, if anyone can see what I did wrong please say so.