用MIDP2.0裡Game包的Sprite精靈類來實現上下左右四個方向的人物移動。主要就是把人物的每組動作歸成一組幀序列數組,根據人物當前的狀態設定相應的幀序列數組。再迴圈切換每組幀序列數組裡的幀,同時改變精靈的座標,並在螢幕上繪製出來。
PS: 執行個體裡所用到的圖片取自網路,如有侵權,敬請告之。
:
MIDlet類略,以下是spriteCanvas類:
package sprite;</p><p>import javax.microedition.lcdui.Canvas;<br />import javax.microedition.lcdui.Graphics;<br />import javax.microedition.lcdui.Image;<br />import javax.microedition.lcdui.game.Sprite;</p><p>/**<br /> * 精靈幀序列<br /> *<br /> * @author kf156(亞日)<br /> *<br /> */<br />public class spriteCanvas extends Canvas implements Runnable {<br />public int[] key = new int[4];</p><p>public final static int KEY_UP = -1;// 上</p><p>public final static int KEY_DOWN = -2;// 下</p><p>public final static int KEY_LEFT = -3;// 左</p><p>public final static int KEY_RIGHT = -4;// 右</p><p>public final static int NONE = 0;// 站立</p><p>public final static int UP = 1;</p><p>public final static int DOWN = 2;</p><p>public final static int LEFT = 3;</p><p>public final static int RIGHT = 4;</p><p>public int direct = NONE, ndirect = NONE;// 當前方向,和下一動作方向</p><p>public int width, height;</p><p>public Thread thread;// 主線程</p><p>public Image image;// 圖片</p><p>public Sprite mSprite;// 精靈</p><p>// 定義幀序列數組<br />int[][] FRAME_SEQUENCE = { { 0, 1, 2, 1 },// 向上走的幀序列<br />{ 3, 4, 5, 4 },// 向下走的幀序列<br />{ 6, 7, 8, 7 }, // 向左走的幀序列<br />{ 9, 10, 11, 10 }, // 向右走的幀序列<br />{ 1 },// 向上站立幀<br />{ 4 },// 向下站立幀<br />{ 7 }, // 向左站立幀<br />{ 10 } // 向右站立幀<br />};</p><p>int curX;// X座標</p><p>int curY;// Y座標</p><p>/**<br /> * 構造<br /> *<br /> */<br />public spriteCanvas() {<br />setFullScreenMode(true);<br />width = getWidth();<br />height = getHeight();<br />init();<br />startThread();<br />}</p><p>/**<br /> * 初始化<br /> *<br /> */<br />public void init() {</p><p>try {<br />image = Image.createImage("/run.png");// 載入Sprite的原映像<br />} catch (Exception e) {<br />System.out.println("load image error:" + e);<br />}<br />mSprite = new Sprite(image, 21, 31);// 建立精靈,指定精靈的寬度為21,高度為31<br />mSprite.setFrameSequence(FRAME_SEQUENCE[5]);// 預設幀序列<br />curX = (getWidth() - mSprite.getWidth()) >> 1;// 擷取精靈放置在螢幕中間X座標<br />curY = (getHeight() - mSprite.getHeight()) >> 1;// 擷取精靈放置在螢幕中間Y座標</p><p>mSprite.setRefPixelPosition(curX, curY);// 設定精靈的位置為螢幕的正中間<br />}</p><p>/**<br /> * 按鍵輸入<br /> *<br /> */<br />public void input() {<br />key[0] = key[2];<br />key[1] = key[3];<br />key[2] = 0;<br />}</p><p>public void update() {<br />mSprite.nextFrame();</p><p>switch (key[1]) {<br />case NONE:// 無按鍵<br />switch (ndirect) {// 判斷下一方向<br />case UP:<br />mSprite.setFrameSequence(FRAME_SEQUENCE[4]);// 設定精靈幀序列<br />break;<br />case DOWN:<br />mSprite.setFrameSequence(FRAME_SEQUENCE[5]);<br />break;<br />case LEFT:<br />mSprite.setFrameSequence(FRAME_SEQUENCE[6]);<br />break;<br />case RIGHT:<br />mSprite.setFrameSequence(FRAME_SEQUENCE[7]);<br />break;<br />}<br />direct = NONE;<br />break;</p><p>case KEY_UP:<br />ndirect = UP;<br />if (direct != ndirect) {// 若方向改變,重新設定幀序列<br />mSprite.setFrameSequence(FRAME_SEQUENCE[0]);// 將精靈序列加到精靈屬性中<br />direct = ndirect;<br />}<br />mSprite.move(0, -4);<br />if (mSprite.getRefPixelY() < -mSprite.getHeight())<br />mSprite.setPosition(mSprite.getRefPixelX(), height);</p><p>break;<br />case KEY_DOWN:<br />ndirect = DOWN;<br />if (direct != ndirect) {<br />mSprite.setFrameSequence(FRAME_SEQUENCE[1]);// 將精靈序列加到精靈屬性中<br />direct = ndirect;<br />}<br />mSprite.move(0, 4);<br />if (mSprite.getRefPixelY() > height)<br />mSprite.setPosition(mSprite.getRefPixelX(), -mSprite<br />.getHeight());<br />break;<br />case KEY_LEFT:<br />ndirect = LEFT;<br />if (direct != ndirect) {<br />mSprite.setFrameSequence(FRAME_SEQUENCE[2]);// 將精靈序列加到精靈屬性中<br />direct = ndirect;<br />}<br />mSprite.move(-4, 0);<br />if (mSprite.getRefPixelX() < -mSprite.getWidth())<br />mSprite.setPosition(width, mSprite.getRefPixelY());<br />break;<br />case KEY_RIGHT:<br />ndirect = RIGHT;<br />if (direct != ndirect) {<br />mSprite.setFrameSequence(FRAME_SEQUENCE[3]);// 將精靈序列加到精靈屬性中<br />direct = ndirect;<br />}<br />mSprite.move(4, 0);<br />if (mSprite.getRefPixelX() > width)<br />mSprite<br />.setPosition(-mSprite.getWidth(), mSprite<br />.getRefPixelY());<br />break;</p><p>}<br />}</p><p>protected void paint(Graphics g) {<br />g.setColor(0);<br />g.fillRect(0, 0, getWidth(), getHeight());<br />mSprite.paint(g);// 繪製精靈</p><p>}</p><p>public void startThread() {<br />if (thread == null)<br />thread = new Thread(this);<br />thread.start();<br />}</p><p>public void run() {<br />int debug = 0;<br />// System.out.println("debug:" + debug++);<br />try {<br />while (true) {<br />// 真機上用此方法<br />long stare = System.currentTimeMillis();<br />// System.out.println("debug:" + debug++);<br />input();<br />// System.out.println("debug:" + debug++);<br />update();<br />// System.out.println("debug:" + debug++);<br />repaint();<br />serviceRepaints();<br />long end = System.currentTimeMillis() - stare;<br />if (end < 150)<br />Thread.sleep(150 - end);<br />}<br />// spriteMidlet.quitApp();<br />} catch (Exception e) {<br />System.err.println("run error:" + e + " debug:" + debug);<br />}<br />}</p><p>/***************************************************************************<br /> * 重載的FullCanvas的按鍵觸發的方法<br /> *<br /> * @param keyCode<br /> * int 系統傳入的按鍵代碼<br /> **************************************************************************/<br />protected void keyPressed(int keyCode) {<br />key[2] = key[3] = keyCode;<br />}</p><p>/***************************************************************************<br /> * 重載的FullCanvas的重複一個按鍵觸發的方法<br /> *<br /> * @param keyCode<br /> * 系統傳入的按鍵代碼<br /> **************************************************************************/<br />protected void keyRepeated(int keyCode) {<br />}</p><p>/***************************************************************************<br /> * 重載的FullCanvas的釋放一個按鍵觸發的方法<br /> *<br /> * @param keyCode<br /> * 系統傳入的按鍵代碼<br /> **************************************************************************/<br />public void keyReleased(int keyCode) {<br />key[3] = 0;<br />}</p><p>}<br />
工程已經上傳,覺得網頁上看不方便的朋友也可直接下載(由於此工程是早期上傳的,所以代碼有些地方注釋不詳):
http://download.csdn.net/source/987436