當用戶從主菜單中選擇"High scores"選項的時候,高分就會顯示出來。高分是顯示在全屏畫布(FullCanvas)實例上的。分數應該在一個屏幕上就顯示完,而不要捲動頁面,因爲這樣會給用戶帶來麻煩。
當然了,高分屏幕也可能會包含一些圖片或者動畫。用戶應該能夠通過按下左功能鍵、右功能鍵、數字鍵或者Send鍵返回主菜單。這個例子不提供任何處理高分的機制。處理高分的兩種典型的方法是通過使用記錄管理服務(RMS)永久保存分數或者通過HTTP連接把高分保存到服務器中。下面的代碼是高分的框架。
import javax.microedition.lcdui.*; import com.nokia.mid.ui.*; public class HighScore extends FullCanvas { private GameMIDlet parent = null; private MainMenu menu = null; public HighScore(GameMIDlet parent, MainMenu menu) { this.parent = parent; this.menu = menu; } protected void paint(Graphics g) { //Paint the high scores here } public void keyPressed(int keyCode) { if (keyCode != KEY_END) { //selection list to the screen parent.setDisplayable(menu); } } } |
6 教學屏幕
當用戶從主菜單中選擇"Instructions"條目的時候,就會顯示出遊戲的規則。遊戲規則文本放置在Form實例中。Form中應該包含用於進入下一條教學規則的命令(例如,標籤"More")和回到主菜單的命令(例如,標籤"Back")。"More"一般由左功能鍵控制,而"Back"由右功能鍵控制。如果教學規則裏包含動畫,那麼這些動畫應該使用全屏畫布(FullCanvas)來顯示。按下左功能鍵將跳過動畫進入下一個教學屏幕。按下右功能鍵,用戶將從動畫中回到主菜單。鍵End將結束應用程序。下面的代碼是文字教學規則和動畫的框架。
//Text instructions. Text can be written in constructor or in own method. //Developer should remember that also instruction texts should be //internationalized import javax.microedition.lcdui.*; public class Instructions extends Form implements CommandListener { //Command for going next instruction if needed private Command more = new Command( Resources.getString(Resources.ID_GAME_MORE), Command.OK, 1); //Command for going back to the main menu private Command back = new Command("", Command.BACK, 2); private GameMIDlet parent = null; private MainMenu menu = null; public Instructions(String title, GameMIDlet parent, MainMenu menu) { super(title); this.parent = parent; this.menu = menu; this.addCommand(back); this.addCommand(more); this.setCommandListener(this); } public void commandAction(Command p0, Displayable p1) { if (p0 == more) { //go to the next if needed e.g animation parent.setDisplayable(new InstructionAnimation(parent)); } else if (p0 == back) { parent.setDisplayable(menu); } } } //Instruction animation import javax.microedition.lcdui.*; import com.nokia.mid.ui.*; public class InstructionAnimation extends FullCanvas { private GameMIDlet parent = null; public InstructionAnimation(GameMIDlet parent) { this.parent = parent; } protected void paint(Graphics g) { //Do the animation here } public void keyPressed(int keyCode) { if (keyCode == KEY_SOFTKEY1) { //go to the next instruction screen if needed } else if (keyCode == KEY_SOFTKEY2) { //selection list to the screen parent.setDisplayable(new MainMenu( Resources.getString ( Resources.ID_GAME_NAME), List.IMPLICIT, parent)); } } } |
7關於(About)屏幕
關於(About)屏幕顯示遊戲製作公司的消息文本或標誌。當用戶從主菜單中選擇"About"選項的時候,就會啓動這個屏幕。和教學規則頁面一樣,關於屏幕頁面如果只需要文本信息的話,那麼可以使用Form來實現。如果需要圖像或動畫,那麼應該使用Canvas或FullCanvas。
//Text "About" code skeleton import javax.microedition.lcdui.*; public class About extends Form implements CommandListener { //Command for going back to the main menu private Command back = new Command("", Command.BACK, 1); private GameMIDlet parent = null; private MainMenu menu = null; public About(String title, GameMIDlet parent, MainMenu menu) { super(title); this.parent = parent; this.menu = menu; this.addCommand(back); this.setCommandListener(this); } public void commandAction(Command p0, Displayable p1) { if (p0 == back) { parent.setDisplayable(menu); } } } |
8 退出
從主菜單中選擇"Exit game"選項來中止遊戲並釋放所有的資源。
9 Resources類
Resources類不是一個用戶界面類,與本文中介紹的其他類不同。這個類處理國際化問題。
/** * A simple class to simulate a resource bundle. * Modify the contents of this class according to the * locales/languages you want your application to support. * In your application, retrieve a string using code such as the * following: * <pre> * <code>String s = Resources.getString(Resources.ID_GAME_NEW); * </code></pre> * Copyright (C) 2002 Nokia Corporation */ public class Resources { // Identifiers for text strings. public static final int ID_GAME_NEW = 0; public static final int ID_GAME_OPTIONS = 1; public static final int ID_GAME_HIGHSCORES = 2; public static final int ID_GAME_INSTRUCTIONS = 3; public static final int ID_GAME_ABOUT = 4; public static final int ID_GAME_CONTINUE = 5; public static final int ID_GAME_BACK = 6; public static final int ID_GAME_MORE = 7; public static final int ID_GAME_EXIT = 8; public static final int ID_GAME_LEVEL = 9; public static final int ID_GAME_SOUNDS = 10; public static final int ID_GAME_VIBRA = 11; public static final int ID_GAME_NAME = 12; // List of supported locales. // The strings are Nokia-specific values // of the "microedition.locale" system property. private static final String[] supportedLocales = { "en", "fi-FI", "fr", "de" }; //NOTE: default language must be the first one //for getString to work! // Strings for each locale, indexed according to the // contents of supportedLocales private static final String[][] strings = { { "New game", "Settings", "High scores", "Instructions", "About","Continue", "Back", "More", "Exit game", "Level", "Sounds","Shakes", "Game name" }, { "Uusi peli", "Asetukset", "Huipputulokset", "Peliohjeet", "Tietoja","Jatka", "Poistu", "Jatka", "Poistu", "Vaikeusaste", "Peli??net", "V?rin?tehosteet", "Pelin nimi" }, { "Nouveau jeu", "Paramètres", "Scores", "Instructions", "A propos","Continuer", "Retour", "Suite", "Sortir", "Niveau", "Sons", "Vibrations", "Jeu nom" }, { "Neues Spiel", "Einstellungen", "Rekord", "Anleitung", "über","Weiter", "Zurück", "Weiter", "Beenden", "Ebene", "Ton", "Vibrationen", "Spiel name" } }; /** * Gets a string for the given key. * @param key Integer key for string * @return The string */ public static String getString(int key) { String locale = System.getProperty("microedition.locale"); if (locale == null) { locale = new String(""); // use empty instead of null } // find the index of the locale id int localeIndex = -1; for (int i = 0; i < supportedLocales.length; i++) { if (locale.equals(supportedLocales[i])) { localeIndex = i; break; } } // not found if (localeIndex == -1) { // defaults to first language, in this example English return strings[0][key]; } return strings[localeIndex][key]; } } |