WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程GameAction的使用范例

GameAction的使用范例
作者:J2MEDEV  来源:J2MEDEV  发布时间:2006-7-25 9:08:39

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();
    }
}

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码