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.

Jun 18

Die 4-Wege Navigation (also das navigieren im Widget ohne den virtuellen Cursor) kann im Widget so funktionieren:
Die Runtime springt von Link zu Link je nachdem in welche Richtung der User auf dem Joypad des Handys klickt. Man kann sich das wie einen unsichtbaren Mauszeiger vorstellen, der dann auf die jeweiligen Navigationselemente fokussiert.
Interessanterweise springt der unsichtbare Zeiger aber auch auf -Tags.

Folgendes Beispiel:

...
< img src="einbild.png">
< a id="quitbutton">Quit
...

Dann müsste der User 2mal auf seinem Joypad nach unten klicken bis der Anchor-Tag aktiviert wird und der Zustand “hover” gesetzt wird. Der unsichtbare Mauszeiger springt erst zu dem Bild, dieses hat keinen “hover”-Zustand also sieht man keine Veränderung, erst beim nächsten Klick sieht man, dass überhaupt etwas beim Klicken passiert.

Das ist nervig und umgehen kann man das, indem man die Bilder z.B. als Div mit einem background-image anzeigt. Dann wird im Beispiel oben das Bild nicht angesprungen, sondern direkt der Link, was dem Handybenutzer auch logischer erscheinen wird.

#einbild_container{
  background-image: url(einbild.png);
  width: bildbreite;
  height: bildhöhe;
  background-repeat: no-repeat;
}


< div id="einbild_container">
< a id="quitbutton">Quit

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).

Weiterlesen »

Jun 01

I am currently learning more on Android development and for me the best way to learn something new is to try it out by myself. It’s the same when I learn new ways of programming: reading books and tutorials is fine, but when I try to build my own apps I learn even more.

So I decided to write a simple game for the Android platform, not that there aren’t already enough games in the Android Market, more because in the last weeks I mostly concentrated on developing games for the Vodafone mobile widget runtime. I made a game called “Space Janitor”, which I now want to port to the Android platform. I would like to extend the game at some points, i.e. making use of the device accelerometer sensors to move the spaceship to the left or to the right, or vibrating the mobile when being hit by an asteroid. That would be real fun.

Another good way to learn is to try to explain the things one learned to others. That was a great way to learn during my university time, because the professors always wanted to have something explained :-) So if one already explained the learned things to others, it was no problem to explain it again to the professor. I now decided to keep track of my development by writing some blog posts about how to develop the game, thus explaing it to others. But I would also very much appreciate your comments on how to improve the quality and the speed of my code as I would call myself just a novice in Android programming. Maybe you’ve also got some more tipps for game improvement, game handling, gameplay, etc.

I will somewhat unregularly post new entries in my blog, so keep in touch.