Very well written explanation, thank you very much. I will implement this as soon as possible :).
EDIT: So I did this, followed Eli’s tutorial (Very helpful by the way) and everything is up and running, but the walking for the mob is all messed up. I used to say to add 1/-1 to the x or y for the movement. That was a very smooth walking speed. Now it is all jerky, jumping forward when I use MOVESPEED * delta. I know this has something to do with delta… Any suggestions?
EDIT 2: When I replace MOVESPEED * delta with just a 1 like it used to be there is still no smooth walking speed. So it has something to do with the loop I am using. Here is the code foe anyone interested:
public void run() {
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
while (inRealGame) {
if (inGame) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000) {
System.out.println("(FPS: " + fps + ")");
lastFpsTime = 0;
fps = 0;
}
gamePhysic(delta);
try {
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
} catch (Exception e) {
e.printStackTrace();
}
}
repaint();
}
}
public void gamePhysic(double delta) {
if (!isFirst) {
room.physic();
mobSpawner();
for (int i = 0; i < currLevel.mobs.length; i++) {
if (currLevel.mobs[i].inGame) {
currLevel.mobs[i].physic((int) delta);
}
}
if (currLevel.levelOver) {
getNextLevel(currLevel);
}
}
}
And the moving for the mob (superclass) :
public void physic(int delta) {
if (walkFrame >= walkSpeed) {
if (direction == right) {
x += MOVESPEED;
} else if (direction == upward) {
y -= MOVESPEED;
} else if (direction == downward) {
y += MOVESPEED;
} else if (direction == left) {
x -= MOVESPEED;
}
mobWalk += 1;
if (mobWalk == Screen.room.blockSize) {
if (direction == right) {
xC += MOVESPEED;
hasRight = true;
} else if (direction == left) {
xC -= MOVESPEED;
hasLeft = true;
} else if (direction == upward) {
yC -= MOVESPEED;
hasUpward = true;
} else if (direction == downward) {
yC += MOVESPEED;
hasDownward = true;
}
if (!hasUpward) {
try {
if (Screen.room.block[yC + 1][xC].groundID == Value.groundRoad) {
direction = downward;
}
} catch (Exception e) {
}
}
if (!hasDownward) {
try {
if (Screen.room.block[yC - 1][xC].groundID == Value.groundRoad) {
direction = upward;
}
} catch (Exception e) {
}
}
if (!hasLeft) {
try {
if (Screen.room.block[yC][xC + 1].groundID == Value.groundRoad) {
direction = right;
}
} catch (Exception e) {
}
}
if (!hasRight) {
try {
if (Screen.room.block[yC][xC - 1].groundID == Value.groundRoad) {
direction = left;
}
} catch (Exception e) {
}
}
if (Screen.room.block[yC][xC].airID == Value.airCastle) {
deleteMob();
loseHealth();
}
mobWalk = 0;
hasUpward = false;
hasDownward = false;
hasLeft = false;
hasRight = false;
}
walkFrame = 0;
} else {
walkFrame += 1;
}
}