一步一步学习midp2。0游戏编程 采用新的 MIDP 2.0 API来编写手机游戏给我们带来了很多乐趣. 对程序员来说 MIDP 2.0相对于MIDP 1.0最大的区别就是图像处理能力大大增强. 当然,使用MIDP 1.0 你也可以写出许多令人激动的游戏—那些对 Atari 2600 记忆犹新的人们对之津津乐道,尽管他们不是那么完美。midp2.0
也许还不能让你使用最新的游戏技术,但是它至少可以让手机中的超级马里或thereabouts 成为现实 本文将通过一些简单游戏 MIDlet向你展示midp2的用法. 并且本文的读者被假定为熟悉java语言的j2me新手. 我的例子向你展示了一个在草原中跳跃的牛仔.这个游戏看起来的确很菜,不过它包含了你在编写大部分游戏时常用到的技术. 文中程序代码在这里下载. 开始咯 如果你还没有下载j2me,先下载一个吧:http://java.sun.com/j2me/download.html. Java 2 Micro Edition
Wireless Toolkit 包含MIDlet 开发环境,模拟器, 还有一些例程.在这里我就不对怎么安装j2me在浪费笔墨了,因为随着环境的不同。安装不尽相同,而且联机文档中已经说的蛮清楚的了. 当你查看demo程序的时候, 你会发现它包括jar文件和一个jad文件. Jar包括了class文件, 资源文件,以及一个 manifest 文件. jad 包含了加载MIDlets必须的一些属性:比如jar的大小, 包含MIDlet的jar的url地址.下面是我的 Hello World 例程中所用到的jad文件内容: MIDlet-1: Hello World, /icons/hello.png,
net.frog_parrot.hello.Hello MMIDlet-Description: Hello World for
MIDP MIDlet-Jar-URL: hello.jar MIDlet-Name: Hello World MIDlet-Permissions: MIDlet-Vendor: frog-parrot.net MIDlet-Version: 2.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0 MIDlet-Jar-Size: 3201 MIDlet-1 (以及MIDlet-2, 等.) 属性 给出MIDlet的名称, MIDlet's 图标的位置, 以及MIDlet类.前两个项目描述了在MIDlet菜单中改MIDlet条目将如何显示.当然 icon 必须存在于jar中,它的用法和 Class.getResource(String
res)中res参数用法一样. 所以在本例中,jar根目录下的icons子目录中一定包含一个名为 的hello.png,它看起来是下面这个样子:
MIDlet-Jar-Size 描述了对应的jar文件的字节数. 如果你使用wtk重新编译程序的话, 你必须手动修改这个属性(译注:不是吧,程序似乎自动会修改的吧) MIDlet-Jar-Size 属性与jar 文件不匹配的话程序不会运行(译注:大伙自己试试吧). (因为老是手动修改jar大小的确是非常麻烦的,所以作者在文章的最后附带了一个build用的script,大伙参考参考吧.) MIDlet-Jar-URL 属性告诉系统从什么地方下载这个 MIDlet jar 。如果jar 文件和 jad 在同一个目录,那么只要写jar的名称就成了. 其他的属性都是说明程序自身的. Hello World 为了说明 MIDlet 的结构,我写了这个 "Hello World". 该 MIDlet 在显示器上显示"Hello World!" ,当按下 "Toggle Msg" 按钮时,字符串被移除. 按下 "Exit" 按钮的话程序会退出.程序包括了两个类: MIDlet 类Hello以及Canvas 类HelloCanvas. 这真的的很简单,大伙儿可以看我在代码中的注释. 这是Hello.java的代码: package net.frog_parrot.hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This
is the main class of the hello world demo. * *
@author Carol Hamer */ public class Hello extends MIDlet implements
CommandListener {
/**
* The canvas is the region of the screen that has been
allotted
* to the game.
*/
HelloCanvas myCanvas;
/**
* The Command objects appear as buttons.
*/
private Command exitCommand =new Command("Exit",
Command.EXIT, 99);
/**
* The Command objects appear as buttons.
*/
private Command newCommand =new Command("Toggle Msg",
Command.SCREEN, 1);
/**
* Initialize the canvas and the commands.
*/
public Hello() {
myCanvas =
new HelloCanvas(Display.getDisplay(this));
myCanvas.addCommand(exitCommand);
myCanvas.addCommand(newCommand);
myCanvas.setCommandListener(this);
}
//----------------------------------------------------------------
//
implementation of MIDlet
/**
* Start the application.
*/
public void startApp() throws
MIDletStateChangeException {
myCanvas.start();
}
/**
* If the MIDlet was using resources, it should release
* them in this method.
*/
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
/**
* This method is called to notify the MIDlet to enter a
paused
* state.
The MIDlet should use this opportunity to release
* shared resources.
*/
public void pauseApp() {
}
//----------------------------------------------------------------
//
implementation of CommandListener
/*
* Respond to a command issued on the Canvas.
* (either reset or exit).
*/
public void commandAction(Command c, Displayable s)
{
if(c ==newCommand) {
myCanvas.newHello();
} else if(c ==exitCommand) {
try { destroyApp(false); notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
}
}
} Here's the code for
HelloCanvas.java: package net.frog_parrot.hello; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; /** * This
class represents the region of the screen that has been
allotted * to
the game. *
*
@author Carol Hamer */ public class HelloCanvas extends
javax.microedition.lcdui.game.GameCanvas {
//---------------------------------------------------------
//
fields
/**
* A handle to the screen of the device.
*/
Display myDisplay;
/**
* whether or not the screen should currently display
the
* "hello world" message.
*/
boolean mySayHello =true;
//-----------------------------------------------------
//
initialization and game state changes
/**
* Constructor merely sets the display.
*/
public HelloCanvas(Display d) {
super(false);
myDisplay =d;
}
/**
* This is called as soon as the application
begins.
*/ void
start() {
myDisplay.setCurrent(this);
repaint();
}
/**
* toggle the hello message.
*/ void
newHello() {
mySayHello =!mySayHello;
// paint the display
repaint();
}
//-------------------------------------------------------
//
graphics methods
/**
* clear the screen and display the hello world message
if appropriate.
*/
public void paint(Graphics g) {
// x and y are the coordinates of the top corner of the
// game's display area:
int x =g.getClipX();
int y =g.getClipY();
// w and h are the width and height of the display
area:
int w =
g.getClipWidth();
int h =g.getClipHeight();
// clear the screen (paint it white):
g.setColor(0xffffff);
g.fillRect(x, y, w, h);
// display the hello world message if
appropriate:.
if(mySayHello) {
Font font =g.getFont();
int fontHeight =font.getHeight();
int fontWidth =font.stringWidth("Hello
World!");
// set the text color to red:
g.setColor(0x00ff0000);
g.setFont(font);
// write the string in the center of the
screen
g.drawString("Hello World!", (w - fontWidth)/2,
(h -
fontHeight)/2,
g.TOP|g.LEFT);
}
} } |