国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

GameAction的使用范例

2019-11-18 16:07:31
字體:
來源:轉載
供稿:網友

GameActionExample描述了如何使用Canvas開發游戲,重點突出了如何將keyCode轉換為GameAction處理用戶的輸入。

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.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沽源县| 新沂市| 化德县| 湄潭县| 资源县| 永安市| 六枝特区| 贵德县| 仪征市| 化州市| 宽甸| 夏河县| 隆尧县| 孝义市| 枞阳县| 兴化市| 庆城县| 东莞市| 汉寿县| 上高县| 瑞丽市| 孝感市| 报价| 辽宁省| 呈贡县| 容城县| 公主岭市| 丹巴县| 红原县| 万载县| 信阳市| 许昌县| 余庆县| 漯河市| 嘉峪关市| 东至县| 萝北县| 汶川县| 中西区| 富裕县| 新巴尔虎左旗|