二、实现游戏的步骤
下图显示的是一个游戏MIDlet在成功安装和运行之后用户界面状态的典型的变化流程。我们想通过一个游戏者的视角来阐述开发移动游戏的过程。
740)this.width=740" border=undefined图3 用户界面状态图表
1 开始游戏
在用户启动MIDlet之后,将显示游戏特定的闪动屏幕。闪动屏幕是FullCanvas的一个实例。它可用于显示一个公司的标志或者用动画形式介绍游戏。除了End键以外的所有键盘事件(MIDlet可用的)都可以跳过闪动屏幕并显示主菜单。还应该设置一个时间限定,能够在一定的时间过后自动跳出闪动屏幕进入游戏屏幕。
GameMIDlet类是游戏的基本类;它处理MIDlet的生命周期并且处理游戏显示。下面的代码是闪动屏幕和游戏MIDlet类的构架。
//Skeleton for the base class of game
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GameMIDlet extends MIDlet {
private Display display = null;
//Splash screen that starts the application
private SplashFullCanvas splash;
public GameMIDlet() {
splash = new SplashFullCanvas(this);
}
protected void startApp() throws MIDletStateChangeException {
if (display == null) {
display = Display.getDisplay(this);
}
//splash screen to the display
setDisplayable(splash);
}
protected void pauseApp() {
}
protected void destroyApp(boolean p0)
throws MIDletStateChangeException {
}
public void setDisplayable(Displayable dl) {
display.setCurrent(dl);
}
}
//Skeleton for the splash screen in Nokia Java Game
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
import com.nokia.mid.ui.*;
public class SplashFullCanvas extends FullCanvas {
private GameMIDlet parent = null;
private MainMenu menu = null;
private Timer timer = null;
public SplashFullCanvas(GameMIDlet parent) {
this.parent = parent;
menu = new MainMenu(
Resources.getString(Resources.ID_GAME_NAME),
List.IMPLICIT, parent);
startTimer();
}
protected void paint(Graphics g) {
//Do the splash screen here
}
protected void keyPressed(int keyCode) {
timer.cancel();
timer = null;
//All key events received set the main menu to the screen
parent.setDisplayable(menu);
}
//Timer for the splash screen. Main menu is set to the display
//after 5 seconds.
private void startTimer() {
TimerTask task =new TimerTask() {
public void run() {
parent.setDisplayable(menu);
}
};
timer = new Timer();
timer.schedule(task, 5000);
}
}
2 主菜单(MainMenu)屏幕
主菜单是包含游戏特定选项的固有目录("Continue"、"New game"、"Options"、"High scores"、"Instructions"、"About"和"Exit game")。"Continue"只有在游戏被暂停的时候才能被显示。当"Continue"显示的时候,它必须是目录列表的第一个元素。主菜单的标题必须是游戏的名称。下面的代码是主菜单的框架。
//Skeleton for the main menu
import javax.microedition.lcdui.*;
public class MainMenu extends List implements CommandListener {
private GameMIDlet parent = null;
private GameFullCanvas game = null;
public MainMenu(String p0, int p1, String[] p2, Image[] p3,
GameMIDlet parent) {
super(p0, p1, p2, p3);
init(parent);
}
public MainMenu(String p0, int p1, GameMIDlet parent) {
super(p0, p1);
init(parent);
}
public void init(GameMIDlet parent) {
this.parent = parent;
this.setCommandListener(this);
//if game paused then "Continue" should be available in
//selection list
if (game != null && game.isPaused()) {
if(!(this.getString(0).equals(
new String(Resources.getString(
Resources.ID_GAME_CONTINUE))))) {
this.insert(0,
Resources.getString(Resources.ID_GAME_CONTINUE),
null);
}
this.setSelectedIndex(0,true);
}
else {
//These must be with or without icons
this.append(Resources.getString(Resources.ID_GAME_NEW), null);
this.append(Resources.getString(Resources.ID_GAME_OPTIONS),null);
this.append(Resources.getString(
Resources.ID_GAME_HIGHSCORES), null);
this.append(Resources.getString(
Resources.ID_GAME_INSTRUCTIONS), null);
this.append(Resources.getString(Resources.ID_GAME_ABOUT), null);
this.append(Resources.getString(Resources.ID_GAME_EXIT), null);
}
}
public void commandAction(Command p0, Displayable p1) {
List lis = (List) p1;
String selected =
lis.getString(lis.getSelectedIndex());
if (selected.equals(Resources.getString(Resources.ID_GAME_NEW))) {
game = new GameFullCanvas(parent, this);
parent.setDisplayable(game);
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_OPTIONS))) {
parent.setDisplayable(
new OptionList(Resources.getString(Resources.ID_GAME_OPTIONS),
List.IMPLICIT,
parent, this));
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_HIGHSCORES))) {
parent.setDisplayable(new HighScore(parent, this));
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_INSTRUCTIONS))) {
parent.setDisplayable(
new Instructions(
Resources.getString(Resources.ID_GAME_INSTRUCTIONS),parent,this));
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_ABOUT))) {
parent.setDisplayable(
new About(
Resources.getString(Resources.ID_GAME_ABOUT),
parent,
this));
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_EXIT))) {
parent.notifyDestroyed();
}
else if (selected.equals(
Resources.getString(Resources.ID_GAME_CONTINUE))) {
if (game != null) {
game.gameContinue();
parent.setDisplayable(game);
}
}
}
}
作者:wayne编译 转贴自:yesky.com