The game engine has many structures, but it is basically implemented in a main loop of the game. The main loop in the program contains the main struct of the program framework. Generally, the program of j2-based contains two class files: MIDlet and Displayable. Generally, I put the main game code in the Displayable class. This class is an event-driven program. There are three main corresponding functions void paint (Graphics g), void keyPressed (int keyCode), and void keyReleased (int keyCode ).
1. Use Runnable and create the main loop of the thread
Generally, the entity's approach is to let the Displayable class implement the Runnable interface, and then create a thread in its constructor to start its run () function, the run function contains the main loop of the game. The following is a piece of code in Xianjian.
Public class GameMIDlet extends MIDlet {
Static GameMIDlet instance;
Display display;
GameDisplayable displayable = null;
Public GameMIDlet (){
Instance = this;
Display = Display. getDisplay (this );
Displayable = new GameDisplayable ();
}
Public void startApp (){
Display. setCurrent (displayable );
}
Public void pauseApp (){
}
Public void destroyApp (boolean unconditional ){
Displayable. running = false;
}
Public static void quitApp (){
Instance. destroyApp (true );
Instance. Policydestroyed ();
Instance = null;
}
}
Public class GameDisplayable extends FullCanvas implements Runnable {
/** Main control thread */
Thread MainThread = null;
/** Game clock interval in milliseconds */
Public static long timeinterval = 20;
Public static boolean Isstable = true;
/* Variable used for game clock */
Public static long timeold = 0;
Public static long timenow = 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 ){
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.