Home > Articles > Programming > Games

Constructing a Mobile Game Skeleton

  • Print
  • + Share This
In this chapter, you will learn here how to build a very simple MIDlet called Skeleton that displays information about the mobile phone as lines of text onscreen. Understanding and building upon this sample MIDlet will allow you to code even more complex games.
This chapter is from the book

NOTE

Released by Namco in 1979, Galaxian was the first major space shooter to serve as a legitimate successor to Space Invaders. Galaxian is also the predecessor to Galaga, which is perhaps the most successful space shooter game of all time. In Galaxian, as in most games of the genre, you control a space ship that can move horizontally along the bottom of the screen and fire vertically at attacking aliens. Galaxian holds a special place in video game history because it is the first arcade game to use true RGB (Red Green Blue) color throughout all its graphics.

Java development of any kind revolves around the Java programming language and a set of APIs that provide support for application services such as GUI components, networking, and input/output. Mobile Java game development is no different in that it relies on a set of APIs for supporting the various pieces of functionality required for MIDlet games that must run in a wireless mobile environment. Understanding these APIs and what they have to offer is critical to becoming a mobile Java game developer. This chapter introduces you to the mobile Java APIs and guides you through the creation of a mobile game skeleton. This skeleton MIDlet serves as a template for you to reuse as you continue to develop games throughout the book.

In this chapter, you'll learn

  • How J2ME programming is broken down into a few different APIs

  • About the internal structure of MIDlets

  • How to build a MIDlet from scratch that displays important game-related information about a mobile phone

  • Prepare a MIDlet for distribution

Exploring the J2ME APIs

Before getting into the coding details of your first mobile phone program, you need a quick primer on the APIs that go into building MIDlets. The MIDP (Mobile Information Device Profile) specification is a set of rules that describe the capabilities and limitations of Java with respect to mobile devices. A significant aspect of these capabilities and limitations is the standard set of classes and interfaces that are available for MIDlet programming. Although the MIDP specification provides a detailed description of the API available for MIDlet development, an additional API is provided by the CLDC (Connected Limited Device Configuration). The MIDP API builds on the CLDC API to provide classes and interfaces that are more specific to mobile information devices. You can think of the CLDC as providing a general Java API for networked devices, whereas the MIDP goes a step further in providing a more detailed API that fills in the specifics left out of the CLDC API for compact wireless devices such as phones and pagers.

Figure 3.1Figure 3.1 A MIDlet must make calls to the CLDC and MIDP APIs to carry out most of its functions.

Why should you care about any of these specifications and APIs? The CLDC and MIDP specifications are important because they explicitly define what classes and interfaces can be used to build MIDlets. Mobile devices are nimble machines that don't have the luxury of megabytes of memory to pack full of application overhead. Knowing this, Sun had to figure out a way to provide a core set of functionality with a useful feature set but without bloating the runtime requirements of mobile devices. Their answer is the two-tier approach that consists of a configuration layered with a more detailed profile. The CLDC API describes the core classes and interfaces required by a general network device, whereas the MIDP API adds the classes and interfaces required by a mobile information device such as a mobile phone. Figure 3.1 shows the relationship between a MIDlet and the respective CLDC and MIDP APIs.

Keep in mind that although the CLDC and MIDP APIs have been carefully thought out to trade off functionality against the memory and resource constraints of mobile devices, they will inevitably come up short in certain situations. This means that you will sometimes have to work a little harder as a MIDlet game developer because you don't have as rich an API to work with as you would if you were doing traditional game programming.

The CLDC API

The majority of the classes in the CLDC API are directly included from the standard J2SE API. These classes and interfaces are practically identical to those that you may be familiar with from traditional Java programming. This portion of the CLDC API is located in packages with familiar J2SE names such as java.lang and java.util. In addition to the classes and interfaces that are borrowed directly from the J2SE API, a few interfaces are unique to the CLDC API. These interfaces deal primarily with networking, which is an area of the J2SE API that is particularly difficult to scale down for the needs of network devices.

The CLDC defines a set of interfaces that facilitate generic networking, and leaves the specifics of implementing these interfaces to the MIDP API. So the CLDC API is logically divided into two parts:

  • A series of packages that serve as a subset of the J2SE API

  • A set of generic networking interfaces

The bulk of the classes and interfaces in the CLDC API are inherited directly from the J2SE API. J2ME requires that any classes or interfaces inherited directly from J2SE must not be changed in any way, which means that the methods and fields are identical to the versions found in J2SE. This makes it easier to learn how to program in J2ME, and it also makes Java code more portable between J2SE and J2ME.

Where the CLDC veers away from the J2SE API is in its support for networking, which is outlined in a generic network framework known as the Generic Connection Framework (GCF). The purpose of the GCF is to define a general network architecture that supports networked I/O and is extremely flexible, and is therefore extensible. The GCF is designed as a functional subset of the J2SE networking classes, which means that features described in the GCF are available in J2SE. The GCF consists primarily of a set of connection interfaces, along with a Connector class that is used to establish the different connections. Both the Connector class and the connection interfaces are located in the javax.microedition.io package. You learn much more about network mobile game programming in Chapter 14, "Mobile Game Networking Essentials."

The MIDP API

A device profile picks up where a configuration leaves off by providing detailed functionality to carry out important tasks on a given type of device. In the case of the Mobile Information Device Profile (MIDP), the type of device is a wireless mobile device such as a mobile phone or pager. It is therefore the job of the MIDP API to take the CLDC API and build on top of it the necessary classes and interfaces that make it possible to build compelling MIDlets such as games.

Similar to the CLDC API, the MIDP API can be divided into two parts:

  • Two classes that are inherited directly from the J2SE API

  • A series of packages that include classes and interfaces unique to MIDP development

Like the CLDC API, the MIDP API borrows from the standard J2SE API. Not surprisingly, the bulk of the MIDP API is new classes and interfaces designed specifically for use in MIDlet programming. Although these classes and interfaces can play a similar role as some of the classes and interfaces in the J2SE API, they are entirely unique to the MIDP API and therefore are carefully designed to solve MIDlet-specific problems. This portion of the MIDP API is divided among several packages, all of which are prefixed with the javax.microedition name:

  • javax.microedition.midlet

  • javax.microedition.lcdui

  • javax.microedition.lcdui.game

  • javax.microedition.media

  • javax.microedition.media.control

  • javax.microedition.io

  • javax.microedition.pki

  • javax.microedition.rms

The javax.microedition.midlet package is the central package in the MIDlet API, and contains only one class: the MIDlet class. The MIDlet class provides the basic functional overhead required of a MIDP application (MIDlet) that can execute on a mobile device. You will continue to learn more about the MIDlet class as you progress through the book and construct more complex MIDlet examples and games.

The javax.microedition.lcdui and javax.microedition.lcdui.game packages include classes and interfaces that support GUI components specially suited for the small screens found in mobile devices. Additionally, there are classes and interfaces that specifically target the development of mobile games. Unique features such as sprite animation and layer management make these packages extremely valuable for mobile game programming. You begin learning about some of the classes in these packages later in this chapter, and continue to dig deeper into them throughout the book.

NOTE

If you happen to have used J2ME before, you'll be interested to know that the javax.microedition.lcdui.game package is entirely new to MIDP 2.0. This is why MIDP 2.0 represents a significant leap forward in making J2ME a viable mobile game technology.

The javax.microedition.media and javax.microedition.media.control packages include classes and interfaces for managing audio within a MIDlet. These packages represent the MIDP 2.0 Media API, which is a subset of the larger Mobile Media API; the full Mobile Media API supports a wide range of media objects such as images, sounds, music, and videos. The media features in the MIDP 2.0 Media API are limited to tone generation and the playback of digital audio effects via wave files. You find out the specifics of playing sound in MIDlet games in Chapter 8, "Making Noise with Tones."

You learned earlier that the CLDC lays the groundwork for networking and I/O with the Generic Connection Framework (GCF). The MIDP API builds on this support with the javax.microedition.io package, which includes several interfaces and classes for establishing wireless network connections and shuttling data back and forth across them. The javax.microedition.pki package is used in concert with the javax.microedition.io package to provide secure network communications. You learn how to carry out game basic networking tasks in Chapter 14, "Mobile Game Networking Essentials."

Because mobile phones don't have hard drives or any tangible file system (yet), you probably won't rely on files to store away persistent MIDlet data. Instead, the MIDP API describes an entirely new approach to store and retrieve persistent MIDlet data: the Record Management System (RMS). The MIDP RMS provides a simple record-based database API for persistently storing data such as high score lists and saved game data. The classes and interfaces that comprise the RMS are all located in the javax.microedition.rms package.

  • + Share This
  • 🔖 Save To Your Account