So I’m trying out a new game loop that I recently wrote and it seems to stutter when simply moving a box across the screen. My last game loop which was a modified version of the game loop from KGPJ stuttered as well but I think that may have been me just messing with it. The loop should be running 30-31 fps and the cube moves 5 pixels every update so I don’t think that’s the problem. Does anyone have any suggestions on making my loop smoother?
public void run()
{
long start = System.nanoTime(),curr;
while(canvas.isRunning())
{
if(!canvas.isPaused())
{
canvas.update();
canvas.draw();
}
while((curr = System.nanoTime() - start) < period)
Thread.yield();
start = System.nanoTime();
}
System.exit(0);
}
Oh and this is how I draw, if it matters. The updating is just advance the cubeX 5 and resetting it to 0 - cubeWidth if cubeX > the width of the canvas. If there is any other code I need to post let me know.
public void draw()
{
do
{
do
{
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,CWIDTH,CHEIGHT);
g.setColor(Color.white);
g.fillRect(cubeX,cubeY,cubeWidth,cubeHeight);
g.dispose();
}while(strategy.contentsRestored());
strategy.show();
}while(strategy.contentsLost());
}