I've been scratching my head on this for a short while now, and I'm really quite confused. Recently, I've been making a game, and I'm now trying to redo the way I had initially created the Camera class
. It's now part of a Thread
, and follows around a specific Location
.
Here is the code:
public void run(){
while (game.isRunning()){
if (!lockLocation) continue;
while (center == null){
EntityHandler handler = game.getEntityHandler();
if (handler == null) continue;
center = (Entity) game.getEntityHandler().getFirst(ObjectType.PLAYER);
}
xOffset = center.getLocation().getRawX() - (Game.WIDTH / 2) + (center.getWidth() / 2);
yOffset = center.getLocation().getRawY() - (Game.HEIGHT / 2) + (center.getHeight() / 2);
}
try { thread.join(); }
catch (InterruptedException e) { e.printStackTrace(); }
}
So this bit of code here works... under certain conditions. Take note of the following lines:
xOffset = center.getLocation().getRawX() - (Game.WIDTH / 2) + (center.getWidth() / 2);
yOffset = center.getLocation().getRawY() - (Game.HEIGHT / 2) + (center.getHeight() / 2);
When I keep the lines as is, only the yOffset
works. When I comment out the yOffset
line, the xOffset
will stop working (thus, both lines do not work).
When I comment out the xOffset
line, much like the commented yOffset
, the yOffset
will no longer work, rendering the entire code useless.
Here's the weirdest part; they will both work when I put a System.out.println()
method. (If one of them is commented and a System.out.println()
is placed, the one that is uncommented will work).
I have no clue why the while
loop is just ignoring certain lines unless its under certain conditions. There's absolutely no reason I can think of for it to do so. Any ideas?