我正在尝试使用本教程创建一个游戏循环。
我试图在初始活动类中实现这一点,如下所示,但我遇到了一些问题。我已经请求了全屏并且没有显示标题功能,但我没有得到它们并且 requestRender 不起作用。
当 "running" 设置为 false 时,游戏循环被跳过,渲染器渲染一帧(rendermode 设置为脏)
这不是它在 running=true 时所做的事情。
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class Practice extends Activity {
private Input input;
private GLSurfaceRenderer glSurfaceRenderer;
private final static int maxFPS = 30;
private final static int maxFrameSkips = 5;
private final static int framePeriod = 1000 / maxFPS;
public final static String TAG = "input";
public boolean running = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
input = new Input(this);
setContentView(input);
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
sleepTime = 0;
while (running) {
Log.d(TAG, "gameRunning");
beginTime = System.currentTimeMillis();
framesSkipped = 0;
this.input.update();
this.input.requestRender();
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(framePeriod - timeDiff);
if (sleepTime > 0) {
try{
Thread.sleep(sleepTime);
}catch(InterruptedException e){}
}
while(sleepTime < 0 && framesSkipped < maxFrameSkips){
this.input.update();
sleepTime += framePeriod;
framesSkipped++;
Log.d(TAG, "Frames Skipped");
}
}
}
}
目前游戏逻辑更新正常,但渲染器根本没有渲染(只是黑屏)
我很确定这只是对代码的简单重新洗牌,但有人有什么建议吗?