0

我在run()方法中有一个简单的游戏循环。该类正在实施Runnable。我只想创建一些简单的计时代码来跟踪播放器的播放时间。假设我想在两分钟后结束游戏。

我不确定我这样做是否正确,因为我在屏幕上显示的时间不是秒,而是long像 1370502588 这样的时间格式。我做错了什么?

以下是方法内的部分代码run()

// Game loop ---------------------------------------
@Override
public void run() {
// TODO Auto-generated method stub

// Time 
long startGameTime = 0;
long milliSeconds;
int seconds;

while (gameRunning) {
  if (!surfaceHolder.getSurface().isValid())
    continue;

  try {
    milliSeconds = System.currentTimeMillis() - startGameTime;
    seconds = (int) (milliSeconds / 1000);
    if (seconds > 120)
      // Game over
      canvas = surfaceHolder.lockCanvas();
      ...

编辑:LogCat

06-06 07:27:06.831: E/Trace(864): error opening trace file: No such file or directory (2)
06-06 07:27:07.601: D/libEGL(864): loaded /system/lib/egl/libEGL_emulation.so
06-06 07:27:07.611: D/(864): HostConnection::get() New Host Connection established 0x2a142400, tid 864
06-06 07:27:07.721: D/libEGL(864): loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-06 07:27:07.721: D/libEGL(864): loaded /system/lib/egl/libGLESv2_emulation.so
06-06 07:27:07.731: D/dalvikvm(864): GC_CONCURRENT freed 232K, 10% free 7430K/8199K, paused 99ms+117ms, total 354ms
06-06 07:27:07.801: W/EGL_emulation(864): eglSurfaceAttrib not implemented
06-06 07:27:07.821: D/OpenGLRenderer(864): Enabling debug mode 0
06-06 07:27:08.121: I/Choreographer(864): Skipped 35 frames!  The application may be doing too much work on its main thread.
06-06 07:27:08.201: W/dalvikvm(864): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
06-06 07:27:08.211: E/AndroidRuntime(864): FATAL EXCEPTION: Thread-88
06-06 07:27:08.211: E/AndroidRuntime(864): java.lang.IllegalArgumentException
06-06 07:27:08.211: E/AndroidRuntime(864):  at android.view.Surface.unlockCanvasAndPost(Native Method)
06-06 07:27:08.211: E/AndroidRuntime(864):  at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:785)
06-06 07:27:08.211: E/AndroidRuntime(864):  at se.test.game.GameLoop.run(GameLoop.java:269)
06-06 07:27:08.211: E/AndroidRuntime(864):  at java.lang.Thread.run(Thread.java:856)
4

2 回答 2

4

代替

long startGameTime = 0;

long startGameTime = System.currentTimeMillis();

那么,您已经拥有的这条线更有意义:

milliSeconds = System.currentTimeMillis() - startGameTime;

在您的版本中,您将当前时间减去 0,这意味着用户自 1970 年 1 月 1 日以来一直在玩。在我的版本中,您从当前时间中减去实际开始时间,结果是时间跨度在这两个时刻之间。

于 2013-06-06T07:16:09.127 回答
0

startGameTime 始终为 0,因为它是本地的 run() 并且永远不会更新。

于 2013-06-06T07:18:47.410 回答