Debrief Summary
So far we have discovered two important concepts regarding game development with Java ME:
• It is really easy to write Java games.
• It is really easy to write Java games badly.
This is not all that surprising. The vast majority of mobile phones in the mass market at the time MIDP 2.0 was designed had a number of built-in limitations. So MIDlets did not have much memory, little local storage space and were rarely paused. Few mobile-phone operating systems are multitasking and MIDlets that lose focus are often terminated outright. A direct consequence is that there is a large code base of Java ME sample code in existence that takes little or no heed of the pause-resume cycle. It is common to see (as above) a start() method on the GameCanvas that is called whenever startApp() is called. In almost all cases, this starts a new game thread without checking for an existing game thread or storing a reference to the game thread at class scope so that it can be shut down in a well-defined manner.
TIP: Always keep a class-scoped reference to the game thread to control its behavior.
To help clarify these issues, try the following exercises:
1. Use the Sun WTK (or whatever IDE you prefer) to start the GhostPong MIDlet and let it run for a few minutes. You can quickly see the degradation in performance as more and more Sprite instances are created. Even though all references to these are removed, there are simply too many allocated too quickly for the garbage collection thread to keep up.
2. Install the MIDlet on a Symbian OS phone (by cable, Bluetooth transfer or PC Suite software). Start the game and then move it into the background. Assuming your phone is not in silent mode, you can hear the game continuing to execute in the background, happily consuming battery power, CPU cycles, RAM and delivering a bizarre user experience. Moving the MIDlet back into the foreground in fact triggers a new game thread instance and very quickly you have a process with a number of unmanaged threads.
TIP: Where possible, try to reuse objects by using a managed pool of instances.
Symbian OS is a fully multitasking operating system which means that Java ME MIDlets need to be designed to acquire and release resources as they are moved in and out of the foreground. For an excellent discussion on this the reader is referred to Section 3.5.1, Section 4.5 and [de Jode 2004b].
Post a comment