Tuesday, 19 July 2011

Where did I start and How To Fix Lunar Lander

Buzz Aldrin removing the passive seismometer f...Image via Wikipedia
Ok. So you've got this idea and your target android device in mind, but you've no idea of what to do next?

My primary OS/s are Windows 7 and Linux (Ubuntu's v11.04) so I can only comment on my experience with these.

The first place to check out is Google's own developer's guide. Following the instructions correctly (mostly), I had my IDE set up and me poking around example projects within an hour (including downloads).

The recommended IDE is Eclipse but there are many others out there. As the tutorial was for Eclipse I've stuck with it and kind-a like it. It has contextual prompting and integrated debugging - which does make life that little bit easier. Eclipse does have the advantage of having Windows and Linux versions, allowing me to work on the same project from both my machines.

For my graphics I use Inkscape, a free and versatile vector designer. For audio I use midi files and Audacity, another excellent free app.

Once you have the tools, you need something to play with. Reading a few of the threads on the forums I heard a lot of chatter about the supplied Lunar Lander example, so thought it a good one to check out.

I thought I had hit gold. Animated sprite handling, user control handling, a basic but functional game thread, multi screen handling.

This would have been the perfect shell for Livestock vs The Undead, apart from the fact that it was a heavily flawed example. It didn't handle some of the most fundamental functions that any android application should, ie home/resume.

To cut a long story short it was losing its hook on the thread when the home button was pressed. So when you restarted the app it didn't know what it should be doing and falls over in a big heap. The following code allows the resumed code to pick up a new thread and continue down its merry way.

public void surfaceCreated(SurfaceHolder holder) {
// added fix -->
if(thread.getState()== Thread.State.TERMINATED){
thread = new LunarThread(mHolder, mContext, mHandler);
// <-- added fix }else { thread.setRunning(true); thread.start(); } }

Unfortunately, this isn't the whole story. As all of the variables that were held within the thread class have now effectively been wiped you will now only see a black screen. ie:

class LunarView extends SurfaceView implements SurfaceHolder.Callback {
class LunarThread extends Thread {

To remedy this you have move all of the variable declarations from within the thread class into the outer class. As inner classes can access outer class variables, you don't have to alter the variables themselves! Phew.

class LunarView extends SurfaceView implements SurfaceHolder.Callback {
class LunarThread extends Thread {

Great. So now all the variables will stay in tact. But we've moved stuff around and in doing so we've broken the LunarLander.java file. Just look for, if you're using Eclipse it'll no doubt be highlighted:


Then change to:


You'll have to delete this inside the public LunarThread(SurfaceHolder surfaceHolder, Context context, Handler handler)

mWinsInARow = 0;
mX = mLanderWidth;
mY = mLanderHeight * 2;
mDX = 0;
mDY = 0;
mHeading = 0;
mEngineFiring = true;

Leaving it in will mean everytime you resume the game it will start the lander in the bottom left with the engine already firing. It was some ill conceived start up code to position the lander in front of the splash screen.

These fixes were put together with the help from here.

A word of warning though. Pressing the home button doesn't actually shut the app down - just hides it, so it will happily chomp on your battery reserves. I would recommend using a clean exit using the finish() function on the button press instead.

Other than this - there is still a silver lining. You can still get the gist of how to structure a game frame, so please read through the code.

Finally: check out those forums, there are answers to just about every question you can think of out there. (Apart from the current one, because that's just silly.)


Enhanced by Zemanta

No comments:

Post a Comment

Your message will appear once it has been approved. Thanks for your interest.