Question

I am writing an android game from the book "Beginning Android Games." The game is called Mr. Nom and its essentially like Snake. I finished writing it, and it worked as expected, however the code from the book only allowed for left and right turns with two buttons (a left arrow and a right arrow) to accomplish that task. I wanted to edit the code to allow for another two buttons (an up arrow and a down arrow) that would allow you to turn up or turn down.

The source code download with the book had a set of images that went along with this particular game. One of the images included the left and right arrows, so I simply opened the image in Paint.Net and rotated it 90 degrees so that the arrows were facing up and down instead of left to right. All of these images are stored in the assets folder and I saved the rotated image as buttonsrotate.png. This is the code for drawing the buttons to the screen:

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);
}

When I run the app, the loading screen will open but as soon as I touch the screen to start the game it stops responding and closes. It only does this if I include the line:

    g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64);

Otherwise the game runs fine. Im sure that the image is in the assets, so I have no idea what is causing the problem. This is the logcat I get when I run the game with the g.drawPixmap(Assets.buttonsrotate, 64, 416, 64, 0, 64, 64); line in the code:

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=     com.badlogic.androidgames.framework.impl.AccelerometerHandler@42b2bd30
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)

Please Help! Its driving me crazy!!

Was it helpful?

Solution

Ok I googled for that GameFramework. Have you added the path to the image in the LoadingScreen.java file? You can see that this is done for all the other assets. So add a line like:

Assets.buttonsrotate = g.newPixmap("buttonsrotate.png", PixmapFormat.ARGB4444);

That should do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top