Game Development Reference
In-Depth Information
Figure 3-3.
The Space Blaster architecture
The main activity class loads the XML UI from the file
main.xml
. The XML UI defines a basic Android
LinearLayout
that draws all the sprites on the game. The position of a sprite is controlled using X and Y
coordinates within the
LinearLayout::onDraw()
method. Note that
SpaceBlasterGame
extends
ArcadeGame
, which in turn extends
LinearLayout
. This inheritance hierarchy allows you to define your
own class to use as a
LinearLayout
, where you can draw by simply overloading the
onDraw()
method
(see Listing 3-1).
Listing 3-1.
User-Defined LinearLayout (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<!-- User Defined Linear Layout -->
<ch03.game.sb.SpaceBlasterGame
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000">
</ch03.game.sb.SpaceBlasterGame>
The XML in Listing 3-1 creates a
LinearLayout
with the ID
ll_absolute
whose width and height will
fill the screen (
fill_parent
). It has a vertical orientation, so child controls will be aligned vertically.
Other utility classes in the project follow:
AudioClip.java
: This sound handler class deals with the device's media player.
•
Tools.java
: This simple class has utility methods to display messages to the user.
•