基于Nokia手机的移动游戏开发步步通(五)
5 高分屏幕当用户从主菜单中选择"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)) {
localeIndex = i;
break;
}
}
// not found
if (localeIndex == -1) {
// defaults to first language, in this example English
return strings;
}
return strings;
}
}
页:
[1]