Basicly, I\'m making a game which to update the players position, it uses this thread:
@Override
public void run() {
while(true) {
System.out.pri
A loop like
while(true) {
updatePos(x, y);
}
would completely hog the CPU. Reason it starts behaving better with a println
is probably because you yield a few hundred cycles per iteration for I/O.
I suggest you add a small sleep-method according to the desired frame rate:
while (true) {
try {
Thread.sleep(10); // for 100 FPS
} catch (InterruptedException ignore) {
}
updatePos(x, y);
}
or, even better, go with an event driven approach with for instance a java.util.Timer. (That would be more Java idiomatic.)