The status conversion in the game design is a part that is both very important and complex. When I was transplanted in the past few days, I saw a public void keypressed (int keycode) has nearly 2000 thousand code, such a lengthy code not only does not comply with OO's thinking more seriously, it will bother our program. These two days use State mode to do a status conversion in a J2ME game. Because only an experiment is only an example, only the start menu and help are converted, and the other status is actually very simple in this framework.
Only two states in DEMO now, both of the functionality of keypressed and Paint is different, and we use state mode to implement the functionality of KeyPressed and Paint in various states.
First define the abstract state STATE interface, this interface specifies the behavior of KeyPressed and Paint
Public interface state {
Public void onkeypressed (int keycode, statemanage _current);
Public void Paint (Graphics G);
}
State Interface KeyPRESSED and PAINT methods, we implements two subclasses HelpMenUState and MainMenustate, indicating the status of the main menu status and Help menu, and implement specific onkeypressed and PAINT methods:
Public class mainmenustate implements state {
/ ** Declaration of variables and initialization part * /
Public void onkeypressed (int keycode, statemanage _current) {
Switch (WhichMenu) {
Case 0:
MainMenuKeyPressed (keycode, _current);
Break;
Case 1:
Break;
}
}
Private void mainmenukeypressed (int keycode, statemanage _current) {
Switch (Keycode) {
Case 1: // key_up = 1;
Menuindex = menuIndex <= 0? Menumain.Length - 1: --MenuIndex;
Break;
Case 6: // Key_down = 6;
Menuindex = menuindex> = menumain.length - 1? 0: MenuIndex;
Break;
Case 8: // key_middle = 8;
IF (MenuIndex == 2)
_Current.SetState (new helpmenustate ());
Break;
}
}
Public void paint (graphics g) {
Switch (WhichMenu) {
Case 0:
DrawMainMenu (g);
Break;
}
}
Private void DrawMainu (Graphics g) {
g.drawImage (imgmenu, 0, 0, 0);
G.SetColor (0, 0, 0);
g.setfont (GameInfo.font_Large);
For (int i = 0; i g.drawstring (Menumain [I], 120, 70 i * 20, Graphics.top GRAPHICS.HCENTER; g.drawImage (ImgIndex, 60, 65 20 * MenuIndex, 0); // Painting INDEX G.SetColor (255, 0, 0); g.drawstring (Menumain [MenuIndex], 120, 70 20 * MenuIndex, Graphics.top | graphics.hcenter; } } Public class helpmenustate imports state { / ** Declaration of variables and initialization part * / Public void onkeypressed (int keycode, statemanage _current) { Switch (WhichMenu) { Case 0: HelpMenuKeyPressed (keycode, _current); Break; Case 1: Break; } } Public void helpMenukeypressed (int keycode, statemanage _current) { Switch (Keycode) { Case 1: // key_up = 1; Menuindex = menuIndex <= 0? Helpmenustring.Length - 1 : --Menuindex; Break; Case 6: // Key_down = 6; Menuindex = Menuindex> = HelpMenustring.Length - 1? 0 : MenuIndex; Break; Case 8: // key_middle = 8; IF (MenuIndex == 4) { _Current.SetState (New MainmenusTate ()); } Break; } } Public void paint (graphics g) { Drawhelp (g); } Private void Drawhelp (Graphics G) { For (int i = 0; i <= 15; i ) { For (int J = 0; j <= 10; j ) { g.drawimage (IMG, 0 24 * I, 0 24 * J, 0); } } G.SetColor (251, 220, 48); g.setfont (GameInfo.font_Large); g.drawstring ("<< Help >>", 120, 40, graphics.top | graphics.hcenter; For (int i = 0; i g.drawstring (Helpmenustring [i], 120, 80 i * 25, graphics.top GRAPHICS.HCENTER; G.SetColor (255, 0, 0); g.drawstring (Helpmenustring [MenuIndex], 120, 80 25 * MenuIndex, Graphics.top | graphics.hcenter; } } Finally, define the instance of stateManage, stateManage as a controller for status conversion: Public class stateManage { State_Current; Public stateManage () { _current = new mainmenustate (); // Start with the main menu }