开发者

When in android should I place main game loop in the initial activity class?

I'm trying to create a game loop using this tutorial.

I have tried to implement this in the initial activity class as shown below but I have run into a few problems. I have requested the fullcreen and no title features as shown but I don't get them and the requestRender does not work.

When "running" is set to false then the game loop is skipped and the renderer renders one frame (rendermode set to dirty)

This is not something that it does when 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");
        }

    }
}
}

At the moment the game logic is updating fine but the renderer is not rendering at all (just a black screen)

I'm pretty sure this is just a simple re-shuffling of code but does anybody have any suggestions?


The root cause of your problem is not letting onCreate() return. This is required in order for the app to operate, this is the thread that handles all UI input and changes. I see a lot of energy here that's unfocused check out the tutorials, basic UI tutorials, read about the Android Lifecycle and watch out for ANR's. They will give you a better understanding about how things work and how to combine your activity with a second thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜