Learn J2ME by game code (1)

xiaoxiao2021-03-06  45

Through the code analysis of a game, learn the development of MIDP from China.

Taking a simple shooting game for explanation, more content involving control operations and displaying operations in the code.

This game consists of 7 classes, which can be divided into

Main class: Planemain.java

Game action class: CORTROL.JAVA

Game Properties Class: Plane.java Bullet.java

Game Auxiliary Class: About.java Cover.java Backdrop.java

First introduce the code of the primary class

Planemain.java

Package npc7776; //

Import javax.microedition.midlet. *;

Import javax.microedition.lcdui. *; // introduce related J2ME package

/ ***************************************************

* Class function introduction: Main screens are used to control realistic boot cover and start game

*************************************************** /

Public Class Planemain

Extends MIDlet

Implements commandListener {// Note One

Private Display Display = NULL DISPLAY;

PRIVATE LIST MainList = NULL;

Private command cmdquit, cmdok; // Note Two

Public Planemain () {

String Option [] = {

"Continue", "New Open One Court", "Maximum Point", "Help", "About"}

Mainlist = new list ("Options", List.Implicit, Option, NULL;

Display = display.getdisplay (this);

Cmdquit = New Command ("Exit", Command.exit, 1);

CMDOK = New Command ("Select", Command.ok, 2);

Mainlist.addcommand (cmdquit);

Mainlist.addcommand (cmdok);

Mainlist.setCommandListener (this);

} // Notes three

Public void startapp () {

Display.Setcurrent (New Cover (this)); // Note Four

}

Public void pauseApp () {

}

Public void destroyApp (boolean unconditional) {

Mainlist = NULL;

Display = NULL;

}

/ **************************************************

* Features: Return to the main interface

* Input parameters:

* Return parameters:

********************************************************* /

Public void goback () {

Display.setCurrent (MainList); // Displays the main screen

} // Note five

Public void CommandAction (Command C, Displayable D) {// Note Six

IF (c == cmdok) {

Switch (Mainlist.getSelectedIndex ()) {

Case 0:

Break; Case 1:

Display.SetCurrent (New Cortrol (this));

Break;

Case 2:

Break;

Case 3:

Break;

Case 4:

Display.SetCurrent (New About (this));

Break;

}

}

IF (c == cmdquit) {

DESTROYAPP (TRUE);

NotifyDestroyed ();

}

} // Note Seven

}

Note:

I. Click on the code: public class planemain extends MIDlet imports commandListener defines a class named PlaneMain, this class passes MIDlet

Delivery, MIDlet is a virtual class, which needs to be overloaded by STARTAPP, PAUSEApp, DestroyApp, which will be called when the MIDlet status changes.

1. When the program runs or restores the run, the StartApp method is called.

2. When the program is suspended, the PASUSEAPP method is called.

3. When the program exits, the DestroyApp method is called.

At the same time, the PlaneMain implements the CommandListener interface, and this interface is to allow the PlaneMain object to handle the menu command from the window.

2.Private display display = null; private list mainlist = null; private command cmdquit, cmdok; here we define the display class, List

Class and two event buttons.

3. PPUBLIC PlaneMain () {} In the construction method, define the contents of List load, set the current form, and define the listening event. MAINLIST.ADDCommand (cmdquit) role

It is to add the command to the mainlist, then call Mainlist.setCommandListener (this) to set the command listener on the PlaneMain object.

4. Show the cover two seconds. STARTAPP Here we start the form display after the form starts. Note We give COVER classes to the current setting. In the COVER class, we only realize the cover specific

The time (Cover class will be written later).

5. The Goback class is called by the COVER class. After the end of the cover, the content is displayed. Here is only understood, and it will be understood after writing the Cover class later.

Sixth.CommandAction method is a method of implementing the CommandListener interface for menu command processing.

7. The entire CommandAction method is mainly listening to the control button action. After selecting the item, click Confirmation to make different processing. When elect it, you can transfer to other pages, select new

Turn the current page to the CORTROL class when you open the game. When you choose about it, the current page control is handed over to the About class. Finally, when we click the exit button, exit the current game, at this time

The destroyApp () method will be called. MainList.getSelectedIndex () means that the location index of the selected option is obtained.

Note: If you write the upper code to JB, some red lines will appear. Because some classes have not been written, it is mainly to understand the procedures, Cover, About, and the CORTROL class will be written in the near future.

J2ME just learning J2ME If you write, please ask you.

转载请注明原文地址:https://www.9cbs.com/read-40740.html

New Post(0)