J2ME Game Development: Issues and Troubleshooting(TM) - Methods in the Main Class
by Massimo Perrone (Hanami Solutions)
3.2 Game Classes
To better organize our project we will have then two classes with their methods
shown as follow:
GameMIDlet.class
startApp();
pauseApp();
destroyApp();
GameCanvas.class
run();
paint();
keyPressed();
keyReleased();
3.3 Methods used in the main class.
It is time to generically explain how should be the content of each method like
to give life to a full game.
Inside the main class first method to take care of is, no doubt, startApp().
Thanks to it we can instance Canvas class of the game and create a straight link
to that class to control the several actions of the game; let's see how.
Whithin the class contructor let's prepare those objects we will use later by
mean of the following instructions:
public GameMIDlet()
{
...
display = Display.getDisplay(this);
IlCanvas = new GameCanvas(this);
...
}
"display" is an object used to phisically link the graphics that we will plot
in our Canvas to the real display of the device. The parameter "this" we've
submitted means that for our MIDlet the default display will be used (that is
the screen of the mobile device). "IlCanvas" instead is an instance of GameCanvas
class whom we will submit the parameter "this", that is the MIDlet we're currently
using (we will see later that this will be useful to make the two classes interact
each other).
Inside startApp() method instead we will use:
public void startApp()
{
...
theCanvas.setCommandListener(this);
display.setCurrent(IlCanvas);
...
}
With the first instruction we will create a listener command to be able to handle
the usage of the several softkeys involved (deploying the effective task to the method
commandAction() ) while with the second instruction, we will declare that what we will
show from now onwards should be fetched from the paint() method of our Canvas class.
The pauseApp() method appears to be quite broad and i won't talk at lenght about
its inner working on this occasion (due to obvious reasons of space) but be aware
that it shows onscreen main menu that is, usually, shown when a given softkey is
pressed (that too controlled by the commandAction() method ordinarily under the name
of "menu" or "settings").
Next Page >>
|