I am developing a Snake game. On all of my devices I have the same speed. Except my Nexus 7. The game just runs slower.
I use System.currenttimemillis()
to count the time for the loop (f.E. 100ms) and get the time how long everything takes to have always the same time every Gameloop. The last thing is Thread.sleep()
till i have my 100ms and then comes the next loop. If there is not enough time for everything to calculate I would have get a Log.d
Message.
On my Nexus 7 i didn't get a Message that it is slow, so it must have the 100ms, but it is much slower than the other devices.
Does anyone have an idea why this could be? Gamefield size is always the same checked that already.
EDIT://
Here is the Code with the GameLoop inside. I think the Thread.sleep
is the problem, but i am not sure.
while (running) {
if (gameEnd) {running = false; Log.d("doInBackground","set running false");}
//Spiel Schleife im OnClickListener, OnClick für Pause
int i = 0;
if (!gameEnd | !gameCancel) {
//Spiel läuft
while (!gameEnd & !gameCancel) {
long timeStart = System.currentTimeMillis();
/*
* LOOP for GamePause
*/
if (gamePause) {
PauseTimeStart = System.currentTimeMillis();
while (gamePause) {
//Spielpause, Dialog einblenden, OnClickListener einrichten mit break
if (!gamePauseDialog) {
gamePauseDialog = true;
gameContinues = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (gamePause)
{
mHandler.sendEmptyMessage(0);}
}
});
}
}
/*
* LOOP for GamePause END
*
* LOOP GameContinue Start
*/
} else {
//Wird das Spiel fortgestzt gibt es eine 2 Sekündige Unterbrechung bevor es weitergeht
if (gameContinues) {
while (gameContinues) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
gameContinues = false;
PauseTime = Math.round(PauseTime + System.currentTimeMillis() - PauseTimeStart);
}
/*
* Start of the normal Gameloop without Cancel, Pause and so on
*/
} else { // Start GameLoop
//Richtungwunsch auf andere Variable übertragen danach auf False setzen
// Dadurch sind die Variablen wieder frei für den nächsten Bewegungswunsch auch wenn Update Game noch nicht
// oder gerade durchgeführt wird
Boolean nDown = NextMoveDown;
Boolean nUp = NextMoveUp;
Boolean nLeft = NextMoveLeft;
Boolean nRight = NextMoveRight;
NextMoveDown = false;
NextMoveLeft = false;
NextMoveRight = false;
NextMoveUp = false;
/*
* Drawing Gamefield, updating GameStatus,Move Snake and so on
*/
gameEnd = GamefieldView.updateGame(
nLeft,
nRight,
nUp,
nDown,
gameContinues);
Points = GamefieldView.getPoints();
//Zeitberechnung
//Time = Math.round(i / FPS); BUGGY
//Durchläufe und Punkte an onProgressUpdate() geben
publishProgress(Points);
i++;
gamePauseDialog = false; //Vorsichtshalber gamePauseDialog false setzen, wenn noch true
/*
* Schwierigkeits grad und Thread Sleep
*/
long tickePS = 1000 / FPS;
long timeSleep = tickePS -(System.currentTimeMillis() - timeStart);
//Log.d("Sleeptime", " " + timeSleep + " : " + timeStart + " : " + System.currentTimeMillis() + " : " + tickePS + " : " + (System.currentTimeMillis()-timeStart));
try {
if (timeSleep > 0) {
Thread.sleep(timeSleep);
}
else {
Thread.sleep(10);
Log.e("PERFORMANCE WARNING", "Loop took too long! No.->" + performanceCounter++);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} //Standard Game Loop END
}
} //If !GameEnd | !GameCancel
} else {
//Wenn Spiel beendet oder abgebrochen wurde
Log.d("GameAsyncTask - ", " Gameover");
}
}