我为Android做了一个2D安卓游戏,游戏运行得非常好,但在关闭后重新打开它,游戏崩溃一次(不幸的是,游戏已经停止工作)再次工作之前。这只发生在较新的设备上,因为我在API级别8较小的屏幕上测试它,但这不会发生。
我怀疑这与我如何使用Bitmap资源进行游戏有关。我不会在代码中的任何地方释放资源,这是我应该做的事情还是垃圾收集器自动处理它?</ p>
除了偶尔的
之外,游戏在所有模拟器设备上重新开启Exception locking surface
java.lang.IllegalStateException: Surface was already locked
重新打开时抛出。我不知道解决这个问题,但很少发生。
以下是MainThread.java类中的代码
while (running) {
startTime = System.nanoTime();
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// draws the canvas on the panel
this.gamePanel.draw(canvas);
}
} catch(Exception e){e.printStackTrace();}finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
启动线程的方法:
@Override
public void surfaceCreated(SurfaceHolder holder) {
player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.helicopter),65,25,3);
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
smoke = new ArrayList<Smokepuff>();
missiles = new ArrayList<Missile>();
botborder = new ArrayList<BotBorder>();
topborder = new ArrayList<TopBorder>();
explosions = new ArrayList<Explosion>();
smokeStartTime = System.nanoTime();
missileStartTime = System.nanoTime();
thread.setRunning(true);
thread.start();
}
和关闭线程的方法:
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
非常感谢任何帮助!