在android中我什么时候应该将主游戏循环放在初始活动类中?

发布于 2024-12-04 13:29:44 字数 2070 浏览 1 评论 0原文

我正在尝试使用本教程创建游戏循环。

我尝试在初始活动类中实现此功能,如下所示,但遇到了一些问题。我已请求全屏且没有标题功能,如图所示,但我没有得到它们,并且 requestRender 不起作用。

当“running”设置为 false 时,游戏循环将被跳过,渲染器渲染一帧(渲染模式设置为 dirty),

这不是 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");
        }

    }
}
}

目前游戏逻辑更新得很好,但渲染器根本没有渲染(只是黑屏)

我很确定这只是简单的代码重新洗牌,但有人有任何建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-12-11 13:29:44

问题的根本原因是不让 onCreate() 返回。这是应用程序运行所必需的,这是处理所有 UI 输入和更改的线程。我在这里看到了很多精力,没有集中精力查看教程,基本的 UI 教程,阅读Android 生命周期并注意ANR。它们将使您更好地了解事物的工作原理以及如何将您的活动与第二个线程结合起来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文