GameAction的使用范例

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

GameActionExample描述了如何使用Canvas开发游戏,重点突出了如何将keyCode转换为GameAction处理用户的输入。

/*

* GameActionExample.java

*

* Created on 2005年12月2日, 下午12:53

*/

package com.j2medev.game;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class GameActionExample extends MIDlet {

PRivate Display display;

private MyCanvas canvas;

public GameActionExample() {

display = Display.getDisplay(this);

canvas = new MyCanvas(this);

}

protected void startApp() {

display.setCurrent(canvas);

}

protected void pauseApp() {

}

protected void destroyApp( boolean unconditional ) {

}

public void exitMIDlet() {

destroyApp(true);

notifyDestroyed();

}

}

class MyCanvas extends Canvas implements CommandListener {

private Command exit;

private String message;

private GameActionExample gameActionExample;

private int x, y;

public MyCanvas(GameActionExample gameActionExample) {

x = 5;

y = 5;

message = "演示游戏动作按键";

this.gameActionExample = gameActionExample;

exit = new Command("退出", Command.EXIT, 1);

addCommand(exit);

setCommandListener(this);

}

protected void paint(Graphics graphics) {

graphics.setColor(255,255,255);

graphics.fillRect(0, 0, getWidth(), getHeight());

graphics.setColor(255, 0, 0);

graphics.drawString(message, x, y, Graphics.TOP Graphics.LEFT);

}

public void commandAction(Command command, Displayable displayable) {

if (command == exit) {

gameActionExample.exitMIDlet();

}

}

protected void keyPressed(int key) {

switch ( getGameAction(key) ){

case Canvas.UP:

message = "向上";

y--;

break;

case Canvas.DOWN:

message = "向下";

y++;

break;

case Canvas.LEFT:

message = "向左";

x--;

break;

case Canvas.RIGHT:

message = "向右";

x++;

break;

case Canvas.FIRE:

message = "开火";

break;

}

repaint();

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航