工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 开发工具 > 移动开发 > 正文
MIDP 2.0 开发J2ME游戏起步之一
[文章信息]
作者:马岩编译
时间:2004-11-16
出处:天极网
责任编辑:方舟
[文章导读]
本文通过对样例游戏Tumbleweed的代码分析,将展示MIDP2.0技术的几个主要部分
advertisement
热点推荐
· 真没想到VB也可以这样用之指针技术
· 禁止QQ登录的方法
· 给你的XML文件做个数字签名
· ImageReady制作“焰火”小动画
· Java加密和数字签名编程快速入门
[正文]

1 2 3  下一页

  MIDP2.0(Mobile Internet Device Profile)技术进行游戏开发中用到的最重要的包是:javax.microedition.lcdui.game,本文通过对样例游戏Tumbleweed的代码分析,将展示MIDP2.0技术的几个主要部分。游戏的主要情景是一个牛仔跳着闪避风滚草,这是一个简单的游戏,但你可以从中学到将来写复杂游戏必须具备的大部分基础知识。

  从MIDlet类开始

  象通常一样,应用从MIDlet类开始。本例中,我的MIDlet子类是Jump。Jump类几乎是完全继承的MIDIet子类,唯一的不同是用到了另一个独立类GameThread,用来动态设置当前窗口允许的有效按钮,比如当游戏处于非暂停状态时,玩家才可以使用暂停这个有效按钮,而激活按钮有效则是在游戏处于暂停状态时候,与此类似游戏停止后,开始按钮才是有效按钮。

  Listing 1是游戏的MIDlet子类——Jump.java源码。

  Listing 1. Jump.java

package net.frog_parrot.jump;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* This is the main class of the Tumbleweed game.
*
* @author Carol Hamer
*/
public class Jump extends MIDlet implements CommandListener {

 //---------------------------------------------------------
 // Commands

 /**
 * The command to end the game.
 */
 private Command myExitCommand = new Command("Exit", Command.EXIT, 99);

 /**
 * The command to start moving when the game is paused.
 */
 private Command myGoCommand = new Command("Go", Command.SCREEN, 1);

 /**
 * The command to pause the game.
 */
 private Command myPauseCommand = new Command("Pause", Command.SCREEN, 1);

 /**
 * The command to start a new game.
 */
 private Command myNewCommand = new Command("Play Again", Command.SCREEN, 1);

 //---------------------------------------------------------
 // Game object fields

 /**
 * The canvas that all of the game will be drawn on.
 */
 private JumpCanvas myCanvas;

 /**
 * The thread that advances the cowboy.
 */
 private GameThread myGameThread;

 //-----------------------------------------------------
 // Initialization and game state changes
 /**
 * Initialize the canvas and the commands.
 */
 public Jump() {
  try {
   myCanvas = new JumpCanvas(this);
   myCanvas.addCommand(myExitCommand);
   myCanvas.addCommand(myPauseCommand);
   myCanvas.setCommandListener(this);
  } catch(Exception e) {
   errorMsg(e);
  }
 }

 /**
 * Switch the command to the play again command.
 */
 void setNewCommand () {
  myCanvas.removeCommand(myPauseCommand);
  myCanvas.removeCommand(myGoCommand);
  myCanvas.addCommand(myNewCommand);
 }

 /**
 * Switch the command to the go command.
 */
 private void setGoCommand() {
  myCanvas.removeCommand(myPauseCommand);
  myCanvas.removeCommand(myNewCommand);
  myCanvas.addCommand(myGoCommand);
 }

 /**
 * Switch the command to the pause command.
 */
 private void setPauseCommand () {
  myCanvas.removeCommand(myNewCommand);
  myCanvas.removeCommand(myGoCommand);
  myCanvas.addCommand(myPauseCommand);
 }

 //----------------------------------------------------------------
 // Implementation of MIDlet.
 // These methods may be called by the application management
 // software at any time, so you always check fields for null
 // before calling methods on them.

 /**
  * Start the application.
 */
 public void startApp() throws MIDletStateChangeException {
  if(myCanvas != null) {
   if(myGameThread == null) {
    myGameThread = new GameThread(myCanvas);
    myCanvas.start();
    myGameThread.start();
   } else {
    myCanvas.removeCommand(myGoCommand);
    myCanvas.addCommand(myPauseCommand);
    myCanvas.flushKeys();
    myGameThread.resumeGame();
   }
  }
 }

 /**
  * Stop and throw out the garbage.
 */
 public void destroyApp(boolean unconditional)
  throws MIDletStateChangeException {
   if(myGameThread != null) {
    myGameThread.requestStop();
   }
   myGameThread = null;
   myCanvas = null;
   System.gc();
  }

 /**
 * Request the thread to pause.
 */
 public void pauseApp() {
  if(myCanvas != null) {
   setGoCommand();
  }
  If(myGameThread != null) {
   myGameThread.pauseGame();
  }
 }

 //----------------------------------------------------------------
 // Implementation of CommandListener
 /*
 * Respond to a command issued on the Canvas.
 * (either reset or exit).
 */
 public void commandAction(Command c, Displayable s) {
  if(c == myGoCommand) {
   myCanvas.removeCommand(myGoCommand);
   myCanvas.addCommand(myPauseCommand);
   myCanvas.flushKeys();
   myGameThread.resumeGame();
  } else if(c == myPauseCommand) {
   myCanvas.removeCommand(myPauseCommand);
   myCanvas.addCommand(myGoCommand);
   myGameThread.pauseGame();
  } else if(c == myNewCommand) {
   myCanvas.removeCommand(myNewCommand);
   myCanvas.addCommand(myPauseCommand);
   myCanvas.reset();
   myGameThread.resumeGame();
  } else if((c == myExitCommand) || (c == Alert.DISMISS_COMMAND)) {
   try {
    destroyApp(false);
    notifyDestroyed();
   } catch (MIDletStateChangeException ex) {
  }
 }
}

//-------------------------------------------------------
// Error methods

/**
* Converts an exception to a message and displays
* the message.
*/
 void errorMsg(Exception e) {
  if(e.getMessage() == null) {
   errorMsg(e.getClass().getName());
  } else {
   errorMsg(e.getClass().getName() + ":" + e.getMessage());
  }
 }

 /**
 * Displays an error message alert if something goes wrong.
 */
 void errorMsg(String msg) {
  Alert errorAlert = new Alert("error", msg, null, AlertType.ERROR);
  errorAlert.setCommandListener(this);
  errorAlert.setTimeout(Alert.FOREVER);
  Display.getDisplay(this).setCurrent(errorAlert);
 }
}


1 2 3  下一页

发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容焦点新闻
  • J2ME 2D小游戏入门之计时器
  • J2ME 2D小游戏入门之实现爆炸
  • J2ME 2D小游戏入门之加入子弹群
  • J2ME 2D小游戏入门之控制飞机移动
  • J2ME 2D小游戏入门之游戏的框架
  • 民营家电商排队造手机 设备商全面杀入
  • 英特尔澄清杨旭任职传闻 官方没宣布此消息
  • 国资委河北密制联通拆分方案
  • 垃圾邮件害人害企害国 清除垃圾邮件不手软
  • 中兴携手阿尔卡特 全球逐鹿CDMA
  • 用友总裁王文京:誓将ERP变成“大众消费”
  • 香港消费者委员会:数码相机最贵未必最好
  • 外电称中兴正评估西门子手机业务 或能并购
  • Advertisement

    天极无线


    奇妙科幻|美好风光|清风车影|漫画卡通|星座生肖|明星写真|动物世界
    老鼠爱大米
    挥着翅膀的女孩
    女人味
    栀子花开
    白月光
    刚刚好
    江南
    快乐崇拜
    亲爱的你怎么不在我身边
    小薇
    2002年的第一场雪
    有多少爱可以重来
    我的地盘
    七里香
    情人
     
    老鼠爱大米 老板电话
    冲动的惩罚 七里香
    我不是黄蓉 女生撒娇
    盛夏的果实 坚持到底
    孤单北半球 眉飞色舞
    挪威的森林 可爱女人
    最浪漫的事 老板电话

    CSEEK搜索