Sprite,精靈,顧名思義,專用來代表游戲中的動畫角色,比如飛機,坦克等等。在MIDP1.0中,我們必須自己寫專門的類來實現Sprite,幸運的是,MIDP2.0為Sprite提供了強力支持,可以創建靜態,動態,不透明和透明的Sprite,下面我們預備在上次的GameCanvas基礎上添加一個Sprite並讓它動起來。
Sprite的主要構造方法有: Sprite(Image):構造一個單幅圖案的Sprite;
Sprite(Image, int width, int height):構造一個動畫Sprite,圖片將按照指定大小被分為N個Frame,通過setFrame(int index)就可以讓Sprite動起來。我們用了一個有透明背景的png圖片創建坦克的Sprite:
(注重這個圖是放大的JPG格式,你需要用Photoshop之類的軟件處理成有透明背景的png格式,大小為64x16)
我們在Eclipse中建立如下工程和目錄:
以下是畫出Sprite的TankGameCanvas.java:
package tank.midp.core;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;public class TankGameCanvas extends GameCanvas implements Runnable { // 控制方向: private static int INDEX_OF_UP = 0; private static int INDEX_OF_DOWN = 1; private static int INDEX_OF_LEFT = 3; private static int INDEX_OF_RIGHT = 2; private boolean isPlay; // Game Loop runs when isPlay is true private long delay; // To give thread consistency private int currentX, currentY; // To hold current position of the 'X' private int width; // To hold screen width private int height; // To hold screen height private Sprite spriteTank; // our sprite! // Constructor and initialization public TankGameCanvas() { super(true); width = getWidth(); height = getHeight(); currentX = width / 2; currentY = height / 2; delay = 20; // init sprite: try { Image image = Image.createImage("/res/img/player1.png"); // 注重路徑 spriteTank = new Sprite(image, 16, 16); // 大小是16x16 } catch(Exception e) { e.printStackTrace(); } } // Automatically start thread for game loop public void start() { isPlay = true; new Thread(this).start(); } public void stop() { isPlay = false; } // Main Game Loop public void run() { Graphics g = getGraphics(); while (isPlay) { input(); drawScreen(g); try { Thread.sleep(delay); } catch (InterruptedException ie) {} } } // Method to Handle User Inputs private void input() { int keyStates = getKeyStates(); // Left if ((keyStates & LEFT_PRESSED) != 0) { currentX = Math.max(0, currentX - 1); spriteTank.setFrame(INDEX_OF_LEFT); } // Right if ((keyStates & RIGHT_PRESSED) !=0 ) { if ( currentX + 5 < width) currentX = Math.min(width, currentX + 1); spriteTank.setFrame(INDEX_OF_RIGHT); } // Up if ((keyStates & UP_PRESSED) != 0) { currentY = Math.max(0, currentY - 1); spriteTank.setFrame(INDEX_OF_UP); } // Down if ((keyStates & DOWN_PRESSED) !=0) { if ( currentY + 10 < height) currentY = Math.min(height, currentY + 1); spriteTank.setFrame(INDEX_OF_DOWN); } } // Method to Display Graphics private void drawScreen(Graphics g) { g.setColor(0); // black g.fillRect(0, 0, getWidth(), getHeight()); // 畫一個Sprite非常簡單: spriteTank.setPosition(currentX, currentY); spriteTank.paint(g); flushGraphics(); }}
運行後的畫面如下,可以用上下左右控制坦克: