I've seen in some other simple 2D games that a "tick" method is used to sync game logic and graphics rendering. My main reason for using this is due to my collision detection malfunctioning, since the player entity will completely pass through solid objects unless I tap the controls almost pixel-by-pixel to collide the rectangles.
I figure a tick method will slow the games rapid updating enough to pick up these slight collision detections.
How do I create such a method? I've tried the code you can see below, however, it is causing the entire game to freeze:
The update method:
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
long lastTime = 0;
long currentTime = 0;
if (state == GameState.Ready) {
updateReady(touchEvents);
}
while (state == GameState.Running) {
lastTime = currentTime;
currentTime = System.currentTimeMillis();
deltaTime = currentTime - lastTime;
player.update(touchEvents, deltaTime);
repaint(deltaTime);
}
if (state == GameState.Paused) {
updatePaused(touchEvents);
}
if (state == GameState.GameOver) {
updateGameOver(touchEvents);
}
}
The repaint method, which is updated while the regular paint method does nothing. I've tried the while loop with both, doesn't seem to stop freezing:
public void repaint(float deltaTime) {
Graphics g = game.getGraphics();
if(deltaTime >= 60) {
g.drawRect(0, 0, 805, 485, color.black);
map.loadMap(getXScroll(), getYScroll(), g, game);
map.loadEntities(getXScroll(), getYScroll(), g, game, map);
g.drawImage(Assets.bitSoldier, 448, 256);
g.drawImage(Assets.dpad, 0, 280);
// Secondly, draw the UI above the game elements.
if (state == GameState.Ready)
drawReadyUI();
if (state == GameState.Running)
drawRunningUI();
if (state == GameState.Paused)
drawPausedUI();
if (state == GameState.GameOver)
drawGameOverUI();
}
}