我正在寫一本Android遊戲,從「開始Android遊戲」開始。這個遊戲叫做Nom先生,它基本上就像Snake。我完成了寫作,並按預期工作,但書中的代碼只允許左右兩個按鈕(左箭頭和右箭頭)完成該任務。我想編輯代碼以允許另外兩個按鈕(向上箭頭和向下箭頭)可以讓你打開或關閉。圖像將不會從資產加載
隨書附帶的源代碼下載了一組圖像,隨着這個特定的遊戲。其中一個圖像包含左右箭頭,所以我只是在Paint.Net中打開圖像並將其旋轉90度,以便箭頭朝上而不是從左到右。所有這些圖像都存儲在assets文件夾中,我將旋轉的圖像保存爲buttonsrotate.png。這是用於繪製按鈕在屏幕的代碼:
private void drawRunningUI() {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.buttons, 0, 0, 64, 128, 64, 64);
g.drawLine(0, 416, 480, 416, Color.BLACK);
g.drawPixmap(Assets.buttons, 0, 416, 64, 64, 64, 64);
g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64);
g.drawPixmap(Assets.buttons, 192, 416, 0, 64, 64, 64);
}
當我運行應用程序,加載屏幕將打開,但只要我觸摸屏幕開始停止響應,並關閉遊戲。它只有這樣做,如果我包括該行:
g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64);
否則遊戲運行良好。我確定圖像在資產中,所以我不知道是什麼導致了問題。這是我用g.drawPixmap(Assets.buttonsrotate,64,416,64,0,64,64)運行遊戲時得到的logcat;在代碼中的行:
01-11 20:25:50.625: D/ActivityThread(9344): setTargetHeapIdealFree:8388608 01-11 20:25:50.625: D/ActivityThread(9344): setTargetHeapConcurrentStart:2097152 01-11 20:25:50.695: V/SoundPoolThread(9344): beginThread 01-11 20:25:50.695: V/SoundPoolThread(9344): run 01-11 20:25:50.715: E/SensorManager(9344): thread start 01-11 20:25:50.725: D/SensorManager(9344): registerListener :: handle = 4 name= MPL accel delay= 20000 Listener= [email protected]b2bd30 01-11 20:25:50.815: I/Adreno200-EGLSUB(9344): : Format RGBA_8888. 01-11 20:25:50.855: E/(9344): : Can't open file for reading 01-11 20:25:50.855: E/(9344): : Can't open file for reading 01-11 20:25:50.985: V/SoundPoolThread(9344): Got message m=2, mData=1 01-11 20:25:50.995: V/MediaPlayer(9344): decode(57, 219601, 9225) 01-11 20:25:51.115: V/SoundPoolThread(9344): Got message m=2, mData=2 01-11 20:25:51.115: V/MediaPlayer(9344): decode(72, 228870, 10222) 01-11 20:25:51.205: V/SoundPoolThread(9344): Got message m=2, mData=3 01-11 20:25:51.205: V/MediaPlayer(9344): decode(74, 153262, 11595) 01-11 20:25:54.709: W/dalvikvm(9344): threadid=14: thread exiting with uncaught exception (group=0x41e41438) 01-11 20:25:54.719: E/AndroidRuntime(9344): FATAL EXCEPTION: Thread-6797 01-11 20:25:54.719: E/AndroidRuntime(9344): java.lang.NullPointerException 01-11 20:25:54.719: E/AndroidRuntime(9344): at com.badlogic.androidgames.framework.impl.AndroidGraphics.drawPixmap(AndroidGraphics.java:104) 01-11 20:25:54.719: E/AndroidRuntime(9344): at com.badlogic.androidgames.mrnom.GameScreen.drawRunningUI(GameScreen.java:194) 01-11 20:25:54.719: E/AndroidRuntime(9344): at com.badlogic.androidgames.mrnom.GameScreen.present(GameScreen.java:134) 01-11 20:25:54.719: E/AndroidRuntime(9344): at com.badlogic.androidgames.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:39) 01-11 20:25:54.719: E/AndroidRuntime(9344): at java.lang.Thread.run(Thread.java:856)
請幫助!這讓我瘋狂!!
你在哪裏申報Assets.buttonsrotate? –