Android开发:游戏后台循环

发布于 2024-12-06 09:29:06 字数 1684 浏览 0 评论 0原文

我正在使用 Canvas/Sur​​faceview 开发 2d 游戏,并且遇到线程问题。 所以我想要完成的是后台的一些事情,例如: 每秒 SpawnEnemy() 或滴答秒数或攻击。

我尝试了 Thread.wait 但这只会造成痛苦并使我的游戏速度达到 2fps。

这是我的 gameLoop:

import android.graphics.Canvas;

public class GameLoopThread extends Thread {
       static final long FPS = 20;
       private GameView view;
       private boolean running = false;

       public GameLoopThread(GameView view) {
             this.view = view;
       }

       public void setRunning(boolean run) {
             running = run;
       }

       @Override
       public void run() {
             long ticksPS = 1000 / FPS;
             long startTime;
             long sleepTime;
             while (running) {
                    Canvas c = null;
                    startTime = System.currentTimeMillis();
                    try {
                           c = view.getHolder().lockCanvas();
                           synchronized (view.getHolder()) {
                                  view.onDraw(c);
                           }
                    } finally {
                           if (c != null) {
                                  view.getHolder().unlockCanvasAndPost(c);
                           }
                    }

                    sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
                    try {
                           if (sleepTime > 0)
                                  sleep(sleepTime);
                           else
                                  sleep(10);
                    } catch (Exception e) {}
             }
       }
}

所以我想要一些在后台滴答作响(秒)的东西,而不是 thread.wait。

I'm developing a 2d Game using Canvas/Surfaceview and have a problem with thread.
So what I want to accomplish is something in the background that is for example:
for each second SpawnEnemy() or ticking seconds or attacking.

I tried Thread.wait but that just cause pain and make my game 2fps.

here is my gameLoop:

import android.graphics.Canvas;

public class GameLoopThread extends Thread {
       static final long FPS = 20;
       private GameView view;
       private boolean running = false;

       public GameLoopThread(GameView view) {
             this.view = view;
       }

       public void setRunning(boolean run) {
             running = run;
       }

       @Override
       public void run() {
             long ticksPS = 1000 / FPS;
             long startTime;
             long sleepTime;
             while (running) {
                    Canvas c = null;
                    startTime = System.currentTimeMillis();
                    try {
                           c = view.getHolder().lockCanvas();
                           synchronized (view.getHolder()) {
                                  view.onDraw(c);
                           }
                    } finally {
                           if (c != null) {
                                  view.getHolder().unlockCanvasAndPost(c);
                           }
                    }

                    sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
                    try {
                           if (sleepTime > 0)
                                  sleep(sleepTime);
                           else
                                  sleep(10);
                    } catch (Exception e) {}
             }
       }
}

So I want something that is ticking in the background (seconds) that doesn't thread.wait.

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

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

发布评论

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

评论(2

幸福%小乖 2024-12-13 09:29:06

您应该使您的线程正常运行游戏约 60fps,请参阅此示例:我如何在画布内使用动画框架?

如果你想每一秒都做一些事情,那么你要么计算帧数onDraw(),在每个第 60 帧执行此操作,或者如果您需要更高的精度,则在每个 onDraw() 中检查系统时间并在一秒过去后执行某些操作。

您还应该考虑使用两种方法,一种用于绘制 onDraw(),另一种用于从线程调用的游戏逻辑。

You should make your thread run the game normally about 60fps, see this example: How can I use the animation framework inside the canvas?

If you want for each second to do something then you either count frames in onDraw(), on each 60th frame do it, or if you need a greater precision then in each onDraw() check the system time and do something when a second has elapsed.

You should also consider using two methods one for drawing onDraw() and another for the game logic which are called from your thread.

嘿咻 2024-12-13 09:29:06

您可以使用 AsyncTask 在后台运行

you could use AsyncTask for something to run in background

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