Jun 01

Game development tutorial on Android – Part 1

First I have to say that I am using Eclipse and the Android plugin to program, because it really minifies the work needed. If your using another IDE you have to change some of the steps according to your IDE’s workflow, but the code stays the same.

I decided that my game should have a welcome screen and an options menu to start the game (or later when playing to pause or stop the game). That is easily done with Android.
First lets create a new project in Android, name it what you like, I chose “Space Janitor” (I assume that you already now how to create new Android project, otherwise refer to one of the many tutorials).


You will then see something like this:

 
package de.rakery.android.spacejanitor.part1;
 
import android.app.Activity;
import android.os.Bundle;
 
public class SpaceJanitor extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

We want to use the full screen for our game and don’t need the window title, so add the following to the onCreate Method:

requestWindowFeature(Window.FEATURE_NO_TITLE);

Press Cmd-Shift-O in Eclipse (sorry, only have a Mac, don’t know which keys on Win/Linux) to automatically import the needed dependencies (here android.view.Window).

Next, lets create the Options Menu to start the game.
We don’t want to keep the Text-Strings in our code, so lets move them to the XML file in res/values/strings.xml.
When using Eclipse you will be presented with the following screen after double-clicking the file:

Editing Strings in Eclipse

So, now you can easily add a new string key-value pair. Add a key “MENU_FIRST” and as value “Start new game” to the strings.xml.
We will later add more menu items, but this one is enough for now.

So how do we add this String as a Menu item to the options menu?
On Android when an activity starts a method onCreateOptionsMenu is called automatically at some points. So we can override the normal method of the class Activity and add our own onCreateOptionsMenu-method.
Lets do this by adding the following right under the onCreate Method

@Override
public boolean onCreateOptionsMenu(Menu menu){
	super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_START, 0, R.string.MENU_FIRST);
	return true;
}

The add-Method takes 4 arguments: the groupId, the itemId, the order, and a title.
As you can see, we set the title by reading R.string.MENU_FIRST (the key we used for the text-string).
I also defined a

private static final int MENU_START = 1;

for my activity, that is now set as the menu itemId, which can later be used to identify user clicks on a menu item.
I’ve set the group ID to 0 because currently there is only one group with the ID zero, and I want to order the items in the order I add them to the menu, so I can set order to 0. If you would like to order in a different way you would have to set the order explicitly.

Now we also want to react on user clicks on a menu item. Once again we override a method defined in Activity called onOptionsItemsSelect:

public boolean onOptionsItemsSelect(MenuItem item){
        switch (item.getItemId()){
	   	// do something with the clicks
	   	case MENU_START:
		// TODO: Define what to do on click
	 }
	 return true;
}

Here we can make use of the constant MENU_START we defined to compare it to the itemID.

Start the emulator and you will see a black screen with a text “Hello World, Space Janitor”. When clicking on the options button, the menu will show up and display the Menuitem “Start new game”.

So, this was part one of the game development on Android tutorial, not much game development here, but we have to start somewhere :-)

4 Antworten zu “Developing an Android mobile game – Part 1”

  1. omg... sagt:

    fix your typos…
    I had to struggle with your typos for hours
    menu.add(0, MENU_START, 0, R.string.MENU_START); —–>
    menu.add(0, MENU_START, 0, R.string.MENU_FIRST);
    onOptionsItemsSelect —-> onOptionsItemSelected

  2. No_Name sagt:

    Could you explain “Add a key “MENU_FIRST” and as value “Start new game”.”

  3. gleisarbeiter sagt:

    Sorry, just updated the Strings.

  4. gleisarbeiter sagt:

    Meaning is: Add a key-value-pair to res/strings.xml

Einen Kommentar schreiben