4-Wege Navigation in der Vodafone Runtime Galaxy Lander – Widget der Woche in Portugal
Jun 24

This is part 2 of my Android mobile game series. I want to develop a game just like my Widget game for the Vodafone Widget Runtime: Space Janitor.

So, in the last part we created some parts of the UI the user sees when he starts the game.

Today we want to create a small part of the real “Game View”!
First, lets change the standard layout of the Android App. The layout file can be found in res/layout/main.xml.
The standard is a simple LinearLayout with a TextView, that currently just displays “Hello…” when you start the game.
Let’s remove this part and change it to:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
    <GameView
    	android:id="@+id/game"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent" />
 
    <RelativeLayout
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
		android:id="@+id/text"
		android:text="@string/space_janitor_info_text"
		android:visibility="visible"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:layout_centerInParent="true"
		android:gravity="center_horizontal"
		android:textColor="#88ffffff"
		android:textSize="24sp" />
    </RelativeLayout>
</FrameLayout>

First, we define a Game View that we can use later on to hold the game graphics. Second we defined a RelativeLayout that holds a TextView, we need this to display some infos later on.

Now let’s extend the code to use this views.

Create a new class called GameView in your App. The GameView extends SurfaceView and has to implement the SufaceHolder.Callback interface. Add the yet unimplemented methods and a constructor to fulfill the interface’s requirements.

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
 
	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
 
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
 
	}
 
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
 
	}
 
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
 
	}
}

The SurfaceView is designated to hold the game graphics. Via the SurfaceHolder.Callback we are informed about dimension changes, i.e. when the user switches to landscape mode.

In the Main View we now have to code a connection to the Game View, we’ll do this via an instance variable of GameView. Add a variable of the type “GameView” to your Activity.

public class SpaceJanitor extends Activity {
	private static final int MENU_START = 1;
	private GameView myGameView;
        ...
}

In our “onCreate”-method we have to initialize this variable and we would like to give the GameView the ability to display some info in the TextView we recently defined in our layout file, like “Game Over”, etc. So:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
 
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   setContentView(R.layout.main);
   myGameView = (GameView) findViewById(R.id.game);
   myGameView.setTextView = (TextView) findViewById(R.id.text);
}

This implies that we have to add a method “setTextView” in the GameView class to save a reference on this TextView locally.

public void setTextView(TextView findViewById) {
		// TODO Auto-generated method stub	
	}

In the next part we will add a thread to handle graphic updates, user actions, etc. etc.

2 Antworten zu “Developing an Android Mobile Game – Part 2”

  1. mike sagt:

    Hey, keep these coming. You have the most comprehensive tutorial on the net (if you’ll keep going).

  2. gleisarbeiter sagt:

    Thank Mike, I try to update as soon as possible. There’s currently so much work to do, I don’t find the time to update :-(
    But there’s a good book about Android Game Programming. Have a look at Ed Burnett’s “Hello, Android”. He programs a simple Sudoku game for the Android in the book.

Einen Kommentar schreiben