站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题基于Nokia手机的移动游戏开发步步通[转贴]

基于Nokia手机的移动游戏开发步步通[转贴]

摘要: 基于Nokia手机的移动游戏开发步步通[转贴]
基于Nokia手机的移动游戏开发步步通

作者:wayne 文章来源:yesky

一、游戏开发策略

  1 游戏动作(Action)的使用

  MIDP的Canvas类让程序员处理某些按键事件,要么作为特定的低级键控代码事件要么作为抽象的游戏动作。FullCanvas是Nokia的全屏画布(FullCanvas)类,它是从Canvas类继承而来的。

  在回合制游戏或者不需要超过四个方向键(上、下、左、右)的游戏中,最好使用直接的键盘代码来控制游戏动作。可以使用抽象游戏动作的游戏例子包括国际象棋和知识测验以及解谜游戏,这些都是使用方向键来滚动屏幕或者移动。

  游戏动作应该只在不需要快速反应的游戏中使用。这是因为所选择的设备的游戏动作映射可能对于要求快速动作的游戏并不适用。并且,如果一个游戏要求斜的方向键或者这个游戏是一个快节奏的动作游戏,那时游戏者一只手需要移动游戏角色,另一只手需要执行其他操作,比如射击、开门等等,那么就需要使用直接键盘代码。原因是在MIDP中没有斜向的游戏动作,并且游戏动作映射是为一只手使用设计的。

  当使用直接键盘代码事件的时候,必须特别注意应用程序的可移植性。

  在不同的设备之间键盘的布局有很大区别(参见图1,是两种手机不同的键盘布局)。开发者可以通过允许用户在游戏中自己定义按键的方式来解决这个问题。这可以在游戏开始之前或者在游戏的"选项"页面中完成。

  键盘代码和游戏动作在同一个应用程序中决不应该混合使用。


图1、不同手机的键盘布局

  2 关于游戏动作的说明

  一个MIDlet应用程序通过调用Canvas方法来探测哪些键盘代码映射到运行的应用程序中的抽象游戏动作:

public static int getGameAction(int keyCode);

  Canvas类定义抽象游戏动作集:UP、DOWN、LEFT、RIGHT、FIRE等等。

  游戏开发者应该知道MIDP 1.0规范中的一个问题。这个类定义了转化键盘代码到游戏动作的方法,同样也定义了转化游戏动作到键盘代码的方法。

public int getGameAction(int keyCode)
public int getKeyCode(int gameAction)

  方法getKeyCode(int gameAction)可能会导致一些问题,因为它只能返回基于游戏动作的一个键盘代码,即使MIDP 1.0允许超过一个键盘代码被实现。在Nokia手机中,个别的一些键盘代码被映射到相同的游戏动作,比如"UP键"和"2键"都被映射为向上的游戏动作。而这个方法只能返回其中之一;返回的值是特定的实现。然而,如果方法getGameAction(int KeyCode)使用"UP键"和"2键"的键盘代码作为参数,这个方法将返回正确的向上的游戏动作。下面来看一个不好的例子,以加深我们的印象:

//不好的例子,不要这么做:
class TetrisCanvas extends Canvas {
int leftKey, rightKey, downKey, rotateKey;
void init (){
//FOLLOWING MUST NOT BE DONE
leftKey = getKeyCode(LEFT);
rightKey = getKeyCode(RIGHT);
downKey = getKeyCode(DOWN);
rotateKey = getKeyCode(FIRE);
}
public void keyPressed(int keyCode) {
if (keyCode == leftKey) {
moveBlockLeft();
} else if (keyCode = rightKey) {
...
}
}
}

  下面是更好的解决方案:

class TetrisCanvas extends Canvas {
void init (){
}
public void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action)
{
case Canvas.LEFT:
moveBlockLeft();
break;
case Canvas.RIGHT:
moveBlockRight();
break;
}
}
}

  这个例子是MIDP 1.0规范中的例子,使用getKeyCode ( int gameAction)处理键盘代码值,只能返回一个值。如果这样的话,其它可能的按键映射就不能在MIDlet中使用了。比如说,在Nokia 7650中就会出现问题,Nokia 7650有五个方向键和一个操纵杆以及普通的键盘布局,上面这个例子就会返回操纵杆的值而不是键盘的值。这是处理事件的一种与设备无关的方法,也是一种不好的方法。更好的解决方法是在keyPressed ()方法内使用getGameAction ( int KeyCode)。通常,应用程序应该避免使用getKeyCode ( int gameAction)方法并且总是使用getGameAction ( int KeyCode)。

3 游戏外壳和游戏动作

  Nokia新型号手机支持的游戏外壳可能会影响游戏动作。Nokia第一款可以使用游戏外壳的手机是Nokia 3510i(参见图2)。如果手机支持游戏外壳,设备的游戏菜单中会有一个设置对话框,允许用户设置游戏外壳上的按键对应的游戏动作。用户必须设置这个按键映射,否则设计使用普通键盘布局的游戏就不能运行。


图2、 Nokia 3510i (左图)原装外壳 (右图)游戏外壳

  4 同时按键

  许多Nokia手机(例如,诺基亚6310i、3410、7210)不支持同时按下多键(也就是说,如果你按住Up键,那么MIDlet就不会知道你是否你按下Fire键)。虽然Nokia 7650支持同时按下多键(例如同时按住"1"和"3"键),但是操纵杆键物理上是无法同时又向左按又向上按的,所以在游戏中不可能有斜向的运动,并且在它被向上、向下、向左或者向右按下的时候是不能记录一个"点击"的。

  5 键名:避免硬编码你自己的文本字符串

  避免把硬编码文本和你的MIDlet中的键盘代码相关联。Canvas类提供了一个返回每个键盘代码关联的文本名称的方法:

public static String getKeyName(int keyCode);

  现在的Nokia设备的问题是这个方法始终返回一个英语字符串,而不是根据用户的语言选择。在未来的Nokia设备中,这个字符串可能会基于用户的语言选择。如果你的目的是开发可移植的游戏,那么必须记住getKeyName在不同的Nokia MIDP设备中返回不同的值。

  6 使用全屏画布(FullCanvas)功能键

  一个基于全屏画布(FullCanvas)游戏屏幕的功能键只能用于从游戏屏幕退出到不同的屏幕状态。带有FullCanvas类的Nokia用户界面应用编程接口明确地提供一个全屏图形区域用于绘图,可用于许多类型的游戏。

  继承于Nokia用户界面应用编程接口类FullCanvas的游戏屏幕将不会把功能键的键盘代码(即KEY_SOFTKEY1、KEY_SOFTKEY2,KEY_SOFTKEY3)用做游戏相关的动作,除非是用于从游戏屏幕返回到菜单中。

  如果一个MIDlet应用程序需要使用功能键命令,那么你必须使用默认的MIDP Canvas类,特别是如果你的MIDlet需要使用标签功能键的时候。当你使用FullCanvas的时候,你不应该提供你自己的功能键标签。

  7 MIDlet国际化

  你有可能需要把你的MIDlet国际化--例如,用于不同的地区和语言。MIDP的系统属性"microedition.locale"定义了设备的当前区域。它的值可以使用System.getProperty.方法取得。MIDP规范允许在这个系统属性中使用一个空值。然而,在Nokia的MIDP实现中是不允许空值的。CLDC的系统属性"microedition.encoding"定义了设备的默认字符编码。它的值可以使用System.getProperty方法取得。
想要了解更多MIDlet本地化问题,可以参阅《Writing World Aware J2ME Applications》http://wireless.java.sun.com/midp/ttips/worldaware/ 的资源包。目前在MIDP中还没有一个标准机制用来处理资源包。这个文档使用一种简单的途径把用户界面文本从主程序中分离出来。它在Resource 类(二、9节)中被处理。

  把国际化特色加入一个MIDlet非常重要,但是这可能会增加你的MIDlet的大小。如果MIDlet大小对于某种特定MIDP设备来说是一个问题,那么你可能希望产生好几个不同编译版本的MIDlet。每个版本可以为一个或多个区域、语言本地化。

  8 设备特性:声音、振动和灯光

  如果你使用Nokia用户界面应用编程接口类Sound或DeviceControl(振动、灯光),你应该提供一个Options或Settings菜单和一个设置编辑器,来允许最终用户启动或者取消这样的设置。

  然而,如果你使用Nokia用户界面应用编程接口类Sound或DeviceControl(振动、灯光)并且通过设置JAD参数<>来把你的游戏安装到Games菜单(见一、11节),Games菜单中已经提供的设置允许最终用户启动或者取消这些特性。因此应用程序没有必要自己创建这样一个特性。除Nokia UI API技术文档之外,你还可以参考《Nokia UI API Programmer ' s Guide》,它能提供很多有用的信息。

  9 设备无关性

  Nokia MIDP设备可能在屏幕尺寸、键盘布局和可用API等方面不同。为了创建可移植的游戏,在设计游戏时,这些差异应当被考虑在内。

  应用程序应该向系统询问屏幕的尺寸,并且避免绘制屏幕内容的时候硬编码坐标。可以使用Canvas类的getHeight和getWidth方法来达到这个目的。

  不同的Nokia MIDP设备中应用编程接口变化很大,开发者应该检查所要开发的设备平台上的应用编程接口。这是可以做到的,振动就是一个很好的例子。

try{
Class.forName("com.nokia.mid.ui.DeviceControl");
}
catch(ClassNotFoundException e){
//Can't use vibration because the API
//is not supported in the device
}

  使用继承于默认MIDP Canvas类的游戏屏幕代替厂商特定的FullCanvas类,这有助于提高你的MIDlet的可移植性;然而,那就不可能实现全屏幕了。

  10 最优化

  MIDP设备的内存非常有限,所以使用内存时应格外小心。对于游戏来说一个很重要的限制就是有限的堆内存(heap memory):为了节省堆内存,对象引用不再需要被设置为"null",以便这些对象可被垃圾-收集(garbage-collected)。彩屏手机需要更多的内存来处理应用程序中的位图,这与更大的屏幕位深度和相关的内部数据结构有关。因此,虽然一个应用程序可能编写来使用在一个黑白屏幕的手机上,但是在彩屏手机上使用时,它可能消耗更多动态内存:就Nokia 7210来说,它显示一幅图片时比Nokia 6310i多用16倍的内存。
开发者应该在设计应用程序时考虑到这个因素,应该把同时加载的图片数降到最少的程度。例如,闪动屏幕图像应该能够在游戏图形图象创建之前被垃圾收集(通过设置所有到图像对象的引用为"null")。

  11 安装

  默认情况下MIDlet被安装到Nokia设备的Applications菜单下。如果设备有Games菜单的话,MIDlet还可以通过设置MIDlet的.jad文件中的Nokia-MIDlet-Category:Game参数来安装到这个菜单下。
二、实现游戏的步骤

  下图显示的是一个游戏MIDlet在成功安装和运行之后用户界面状态的典型的变化流程。我们想通过一个游戏者的视角来阐述开发移动游戏的过程。


图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);
   }
  }
 }
}
3 游戏屏幕

  如果用户从主菜单中选择"New game",那么开始游戏并且显示游戏屏幕。游戏屏幕使用全屏画布(FullCanvas)。如果按下任何功能键,那么用户界面必须返回主菜单,并且应使游戏暂停。其他的按键对游戏是有效的。注意:游戏不应该在屏幕上创建任何功能键的标签。如果必须使用功能键标签,那么应用程序应该使用默认的Canvas屏幕Commands。示例代码没有解决诸如线程和线程安全等问题,这些问题在设计的时候必须格外注意。下面的代码是游戏屏幕的框架。

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
public class GameFullCanvas extends FullCanvas {
private GameMIDlet parent = null;
private MainMenu menu = null;
private boolean gamePaused = false;
public GameFullCanvas(GameMIDlet parent, MainMenu menu) {
this.parent = parent;
this.menu = menu;
}
protected void paint(Graphics g) {
//Paint the game screen here
}
protected void keyPressed(int keyCode) {
if (keyCode == KEY_SOFTKEY1 || keyCode == KEY_SOFTKEY2
|| keyCode == KEY_SOFTKEY3) {
gamePaused = true;
//main menu to the screen
menu.init(parent);
parent.setDisplayable(menu);
}
}
public void gameContinue() {
gamePaused = false;
}
public boolean isPaused() {
return gamePaused;
}
}

  4 游戏选项屏幕

  用户可以通过选择主菜单中的"Options"选项改变特定的游戏选项。Options列表是固有的列表,包含处理游戏设置的条目,例如:声音、振动(见一、8节)、音调等等。如果要回到主菜单的话,需要使用Back命令。下面的代码是Options列表的框架。

  注意:如果游戏被安装到Games菜单的话,就不需要lights/sounds设置条目了,因为那些选项已经由Games菜单提供了。

import javax.microedition.lcdui.*;
public class OptionList extends List implements CommandListener {
private GameMIDlet parent = null;
private MainMenu menu = null;
private KeyDefinitions def = null;
private Command back = new Command("", Command.BACK, 2);
public OptionList(String p0, int p1, String[] p2, Image[] p3,
GameMIDlet parent, MainMenu menu) {
super(p0, p1, p2, p3);
this.menu = menu;
init(parent);
}
public OptionList(String p0, int p1, GameMIDlet parent,
MainMenu menu) {
super(p0, p1);
this.menu = menu;
init(parent);
}
private void init(GameMIDlet parent) {
this.parent = parent;
this.addCommand(back);
this.setCommandListener(this);
//These are just a examples for the game specific options
this.append(Resources.getString(Resources.ID_GAME_LEVEL),
null);
this.append(Resources.getString(Resources.ID_GAME_SOUNDS),
null);
this.append(Resources.getString(Resources.ID_GAME_VIBRA),
null);
}
public void commandAction(Command p0, Displayable p1) {
if (p0 == back) {
parent.setDisplayable(menu);
}
else {
List lis = (List) p1;
int idx = lis.getSelectedIndex();
switch (idx) {
case 0:
//TODO
break;
case 1:
//TODO
break;
case 2:
//TODO
break;
case 3:
parent.setDisplayable(def);
break;
//More if needed
default:
break;
}
}
}
}
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:
*
* String s = Resources.getString(Resources.ID_GAME_NEW);*

* 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];
}
}



2006-11-26 08:48 PM

↑返回目录
前一篇: 基于NOKIA的Camera MIDlet设计 [转贴]
后一篇: J2ME程序开发新手入门九大要点(转贴)