J2ME has developed a wide range. But the rough framework is still regular, I will give you some opinions according to the development experience, do the total
Note: There are many games, but it is basically implemented in a game main loop. The main loop in the program contains the run
The most important structure of the order frame. J2ME's procedures generally contain two class files, one is MIDlet, one is
DISPLAYABLE. Generally, I put the main code of the game in the Displayable this class. This class is based on events.
Driven procedures, there are three main respective functions void Paint (Graphics G), Void KeyPressed (int keycode),
Void KeyReleased (Int Keycode).
1. Use runnable and create threads of the primary loop
The general subject is to let DISPLAYABLE to implement Runnable interface, then create in its constructor.
A thread launches its Run () function, and the RUN function contains the main loop of the game.
Just find a typical code, you will refer to the speech:
Public Class Gamemidlet Extends Midlet {
Static gamemidlet.
Display display;
Gamedisplayable Displayable = null DISPLAY
Public gamemidlet () {
Instance = THIS;
Display = display.getdisplay (this);
Displayable = new gamedisplayable ();
}
Public void startapp () {
Display.Setcurrent (Displayable) and DISPLAYABE
}
Public void pauseApp () {
}
Public void destroyApp (boolean unconditional) {
Displayable.running = false;
}
Public static void quitApp () {
Instance.destroyApp (True);
Instance.notifyDestroyed ();
Instance = NULL;
}
}
Public Class Gamedisplayable Extends Fullcanvas Implements Runnable
{
/ ** Main control thread * /
Thread mainthread = NULL;
/ ** Game clock interval milliseconds * /
Public static long timeInterval = 20;
Public static boolean isstable = true;
/ * Variables for game clock * /
Public static long timeold = 0;
Public static long timenuw = 0;
PUBLIC long interval = 0;
Public static long frames_per_second = 0;
INT count = 0;
Long Second = 0;
Public static boolean running = true;
Public gamedisplayable () {
// Start the main thread
Thread MAINTHREAD = New Thread (this);
MAINTHREAD.START ();
}
Public void run () {
While (Running) {
TimeNow = system.currenttimemillis ();
Interval = TimeNow - TimeOLD;
IF (Interval> = TimeInterval) {TimeOLD = TIMENOW;
Game_process ();
IF (SECOND! = (System.currentTimeMillis () / 1000)) {
SECOND = system.currenttimemillis () / 1000;
FRAMES_PER_SECOND = COUNT;
count = 1;
}
Else
COUNT ;
}
LIB.SLEP (30);
}
}
2. Main loop approach to the thread
This approach can only be implemented on the NOKIA platform, and I only recommend doing on the platform of Nokia 40, so you don't need threads.
In order to save some memory, if it is not a model, it is best to use the previous method.
The game's main loop is placed in the MIDlet's class, and the specific practice is as follows:
Public Class Gamemidlet Extends Midlet {
Gamedisplayable Displayable = null DISPLAY
/ ** Game clock interval milliseconds * /
Public static long timeInterval = 0;
// Variable for the game clock
Public static long timeold = 0;
Public static long timenuw = 0;
PUBLIC long interval = 0;
Public static long frames_per_second = 0;
INT count = 0;
Long Second = 0;
Public Static Boolean Running = FALSE;
Static Boolean EXITAPP = FALSE;
Public gamemidlet () {
Displayable = new gamedisplayable ();
Running = true;
}
Public void startapp () {
Running = true;
Display.getdisplay (this) .Setcurrent (Displayable); DISPLAYABLE
While (Running) {
TimeNow = system.currenttimemillis ();
Interval = TimeNow - TimeOLD;
IF (Interval> = TimeInterval) {
TimeOLD = TIMENOW;
Displayable.game_Process ();
IF (SECOND! = (System.currentTimeMillis () / 1000)) {
SECOND = system.currenttimemillis () / 1000;
FRAMES_PER_SECOND = COUNT;
count = 1;
Else
COUNT ;
}
}
IF (exitApp) {
DESTROYAPP (TRUE);
NotifyDestroyed ();
}
}
Public void pauseApp () {
Running = false;
}
Public void destroyApp (boolean unconditional) {
Running = false;
}
Public static void quitApp () {
Running = false;
EXITAPP = True;
}
}
Part of the code reposted .....