尝试制作非常简单的游戏循环,屏幕上有一个球(使用插值)。
游戏循环:
private double FPS = 50;
private double TIME_STEP = 1000000000L/FPS;
private double MAX_TIME = 1000000000L/5;
Canvas canvas;
double frameStart = System.nanoTime();
double currentTime = 0;
double accumulator = 0;
float ratio = 0;
while (running){
currentTime = System.nanoTime();
accumulator += currentTime - frameStart;
frameStart = currentTime;
if(accumulator > MAX_TIME){
accumulator = MAX_TIME;
}
while (accumulator > TIME_STEP){
screen.update();
accumulator -= TIME_STEP;
}
ratio = Math.min(1f, (float)(accumulator / TIME_STEP));
canvas = surfaceHolder.lockCanvas();
screen.render(ratio, canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
}
更新方法:
public void update(){
pY = cY;
cY -= 0.1f
}
渲染方法:
public void render(float ratio, Canvas canvas){
canvas.drawCircle(cX * ratio + pX * (1 - ratio),
cY * ratio + pY * (1 - ratio),
cR * ratio + pR * (1 - ratio), paint);
}
这些都不能在我的设备上平滑,为什么?我将FPS更改为25和60,但仍然没有平滑。没有插值游戏循环看起来更好,但它是错误的方式。