Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid FPS, how to make it smoother?
    text
    copied!<p>I am having trouble in constructing a basic game loop for my game. I am at the early phase and all I want to do is to move the the ball at a constant speed (for ex: 3) through the bottom of the screen. The position of the ball is updated in the main panel update method and it is drawed in the render part as expected. Nothing exceptional in the mechanism. The ball moves but it is definitely not smooth, the visual is quiet disturbing.. I measured the implementation times of these methods and the total implementation time of this couple is about 3-4 milliseconds. Under these circumstances, what is the suitable FPS? Are the given constant values suitable What is missing or wrong in my mechanism? Thanks a lot in advance. the code block in main thread.</p> <pre><code>private final static int MAX_FPS = 200; private final static int FRAME_PERIOD = 1000 / MAX_FPS; private final static int MAX_FRAME_SKIPS = 5; public void run() { initTimingElements(); long beginTime; long timeDiff; int sleepTime; int framesSkipped; long beginTime2; long diff1; long diff2; sleepTime = 0; Canvas canvas; Log.d(TAG, "Starting game loop"); while (running) { canvas = null; try { canvas = this.surfaceHolder.lockCanvas(); synchronized (surfaceHolder) { beginTime = System.currentTimeMillis(); framesSkipped = 0; this.gamePanel.update(); diff1=System.currentTimeMillis()-beginTime; beginTime2=System.currentTimeMillis(); this.gamePanel.render(canvas); diff2=System.currentTimeMillis()-beginTime2; timeDiff = System.currentTimeMillis() - beginTime; sleepTime = (int) (FRAME_PERIOD - timeDiff); if (sleepTime &gt; 0) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } } while (sleepTime &lt; 0 &amp;&amp; framesSkipped &lt; MAX_FRAME_SKIPS) { this.gamePanel.update(); sleepTime += FRAME_PERIOD; framesSkipped++; } if (framesSkipped &gt; 0) { Log.d(TAG, "Skipped:" + framesSkipped); } framesSkippedPerStatCycle += framesSkipped; storeStats(); } } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload