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.

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.

The odd rows show the player's location, both X and Y. The even rows show x-400, x+400, y-400, y+400.

Edit: Just tested with the enemy's location instead of the player's and it seems that the enemy only appears when it's 300 away from the player. I'm not sure why it's 300 instead of the 400. Bleh, this is confusing.

This doesn't make any sense. You say that your xLocation variable is the player x and your yLocation variable is the player y. However, your "x" variable is already your player x and your "y" variable is also already your player y...

This doesn't make any sense. You say that your xLocation variable is the player x and your yLocation variable is the player y. However, your "x" variable is already your player x and your "y" variable is also already your player y...

Add system.out.println statements to display the value of renderMe after the if statement, and add some in every step of your drawing function to see where things stop.

I added one in the render code and after it, they're always the same. Putting them in the various steps of the render code wouldn't show anything other than true because the render code only runs when renderMe is true.

I know the way that I have everything set up and no matter how I think about this, there should be no reason for this to not be working. It's getting the position of the player, doing some simple math, checking if a few values are higher or lower than others and then changing a boolean. The fact that this isn't working is just weird to me...

If you want a square then for Gods sake use shapes/squares/rectangles that java2d gives you. They have a nice intersects method. I could post the code for both but that would not really help you learn so take this and have at it.

I wonder if one thing you can do is put the player in the middle of the screen and then place an enemy at every single pixel on the screen. If renderMe is true then draw a red pixel for that enemy, if renderMe is false then draw a gray one. This will give you the full picture of where your algorithm is working and where it isn't.

You don't necessarily have to put an actual enemy, just iterate through all of the points on the screen.

Note that, that all can be done in one line but I use many for the sake of clarity. There is also an optimization that is not all necessary but might be something to look into. You will probably figure it out. Basically, stay away from anything that is not addition, subtraction, or multiplication.

//Split up the readout... so that you can read it easier.System.out.println("-------------------------------------");

Replace this code snippet with the one you currently have. This should be able to tell you exactly where your code is failing. (Either that, or the code will work...) Either way, it should be much more informative in figuring out what is going wrong, because that code is 100% accurate in my book.

I don't understand why it isn't rendering. On line 606 of your pastebin console, the X location of the enemy is within range and the renderMe is set to true. On line 4812, it is set to false again. Just like it should.

Stick out your hand. * slaps it*NO!The semicolon on the end of your if-statement terminates it, turning the code below into a scope block

@Stumpy: don't use sqrt. Squaring maxdistance is faster since we don't care what the actual distance is, just if it's lower than 400 units.

@tyee111: if you're in eclipse try stepping through and watching the value of render me. I believe there was a way to set conditional breakpoints, so you could use one to detect when render me is true, then stepping through will reveal where your problem is.

Note that, that all can be done in one line but I use many for the sake of clarity. There is also an optimization that is not all necessary but might be something to look into. You will probably figure it out. Basically, stay away from anything that is not addition, subtraction, or multiplication.

@Stumpy: don't use sqrt. Squaring maxdistance is faster since we don't care what the actual distance is, just if it's lower than 400 units

I used it for the sake of clarity and left that part out as something he could figure out on his own. Please try and read everything someone says as the lack of communication can cause errors. I know that sometimes I type too fast.

tyeeeee1: Now I´m confused. First you stated that renderMe is always set to false in your if statement. Then you posted the result and you´re having a lot of true when it´s supposed to be true. So... Problem solved?

@StumpyStrustI somewhat understand what you're doing there; you're calculating how far away the enemy is from the player and then, if it's further than the max distance, don't render it... right? I'll try that out in a second here.

Edit: I've tried implementing what you've shown me into the code and it ended up working exactly the same as the code I was using before. The enemy only shows up outside of the 400 area around the player and he doesn't show up if I go West or North.

@ctomiI've switched my code with yours and now I can see that the code isn't behaving at all as it should. If I move East or South, the enemy will eventually appear; but no matter how war West of North I go he won't appear. The enemy also seems to only appear outside of the 400x400 area where he should only appear in.

Here is the entire NetBeans project file. The two classes being used are NonPlayer.java (lines 31-56) & ScreenGame.java (line 205). If you want to test out the code for yourself just run the project.http://www.mediafire.com/?s4zt4hv9898fqya

@ deepthoughtI'm on NetBeans. =/ Thanks anyways!

@Everyone talking about a semicolonI must have mis-typed an older version of this code before CnPing it here, the code I'm using doesn't have the semicolon that you were all pointing out.

@AxemanIf you check out the mediafire link above and run the code then you'll see what I mean about the code not working...

@Everyone talking about a semicolonI must have mis-typed an older version of this code before CnPing it here, the code I'm using doesn't have the semicolon that you were all pointing out.

See my post above. I was didn't see a semicolon in my code, and therefore I thought something with the JRE/Java had gone wrong. Ironically, I was tested it on another computer, and because I typed it out instead of copy-pasting it it, the semicolon wasn't there, so (obviously) the code ran, making me even more suspicious of my java installation. In the end, it was just me mis-typing, and then not noticing, because I was so focused on the actual math.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org