所以我有一个带有 TextView 的活动来保存分数,如下所示:
public class PlayGameActivity extends Activity {
private GameThread mGameThread;
private GameView mGameView;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
final TextView scoreView = (TextView) findViewById(R.id.textView2);
mGameView = (GameView) findViewById(R.id.gameView1);
mGameThread = mGameView.getThread();
mHandler.post(new Runnable() {
@Override
public void run() {
if (mGameView != null) {
scoreView.setText(mGameThread.getScore());
}
}
});
}
}
然后在我的 GameView 我有这样声明的函数:
class GameView extends SurfaceView implements SurfaceHolder.Callback {
class GameThread extends Thread {
int score = 0;
public String getScore(){
return Integer.toString(score);
}
@Override
public void run() {
synchronized (mSurfaceHolder) {
if (something){
score++
}
}
}
}
}
问题是 mGameThread.getScore() 总是返回 0,就好像分数没有更新一样。
如果有人有任何煽动,将不胜感激。此外,我从上面的片段中删除了许多不必要的代码。如果您想查看全文,您可以在这里查看: