手机小游戏,地图可以显示,但是精灵无法显示,请大家帮忙改一下吧,
程序代码:
import javax.microedition.import javax.microedition.import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class GameTest extends MIDlet implements CommandListener{ Display dis; GameTestDome gtd; Command cmdOK,cmdPAU,cmdExit; public GameTest() { // TODO Auto-generated constructor stub cmdOK=new Command("开始",Command.SCREEN,1); cmdPAU=new Command("暂停",Command.SCREEN,1); cmdExit=new Command("退出",Command.EXIT,1); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub dis=Display.getDisplay(this); gtd=new GameTestDome(); gtd.addCommand(cmdOK); gtd.addCommand(cmdExit); gtd.setCommandListener(this); dis.setCurrent(gtd); } public void commandAction(Command cmd, Displayable displayable) { // TODO Auto-generated method stub if(cmd==cmdOK){ gtd.removeCommand(cmdOK); gtd.addCommand(cmdPAU); gtd.th=new Thread(gtd); gtd.th.start(); gtd.RUN=true; } if(cmd==cmdPAU){ gtd.removeCommand(cmdPAU); gtd.addCommand(cmdOK); gtd.RUN=false; gtd.th=null; } if(cmd==cmdExit){ try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }import import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.game.Sprite; import javax.microedition.lcdui.game.TiledLayer; class GameTestDome extends GameCanvas implements Runnable{ boolean RUN=true; Thread th; Graphics gra; Sprite sp1,sp2; Image img1,img2,img3; boolean TE; TiledLayer Road,Wall; int position[][]; int y=10; protected GameTestDome() { super(false); // TODO Auto-generated constructor stub try { img1=Image.createImage("/Person2.png"); img2=Image.createImage("/map.png"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //sp1=new Sprite(img1); //sp1.setPosition(this.getWidth(), this.getHeight()); sp1=new Sprite(img1,img1.getWidth()/10,img1.getHeight()/7); sp1.setPosition(this.getWidth(), this.getHeight()); sp1.defineReferencePixel(sp1.getWidth()/2, sp1.getHeight()/2); Wall=new TiledLayer(8,8,img2,img2.getWidth()/2,img2.getHeight()); Road=new TiledLayer(8,8,img2,img2.getWidth()/2,img2.getHeight()); position=new int[][]{ {1,2,1,2,1,2,1,2}, {1,1,1,1,1,2,1,2}, {1,2,1,2,1,2,1,2}, {2,2,2,2,1,2,1,2}, {1,2,2,2,1,2,1,2}, {2,2,2,2,1,2,1,2}, {1,2,1,2,1,2,1,2}, {1,2,1,2,1,2,1,2} }; for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(position[i][j]==1)Road.setCell(j, i, position[i][j]); else Wall.setCell(j, i, position[i][j]); } } gra=this.getGraphics(); th=new Thread(this); th.start(); } public void run() { // TODO Auto-generated method stub while(RUN){ gra.setColor(255, 255, 255); gra.fillRect(0, 0, this.getWidth(), this.getHeight()); String str="top"+y; if(sp1.collidesWith(Wall, true)){ TE=false; y--; gra.drawString(str, 0, 0, Graphics.TOP|Graphics.LEFT); } sp1.nextFrame(); int state=this.getKeyStates(); switch(state){ case GameCanvas.LEFT_PRESSED: sp1.move(-5, 0);break; case GameCanvas.RIGHT_PRESSED:sp1.move(5, 0); break; case GameCanvas.UP_PRESSED: sp1.move(0, -5);break; case GameCanvas.DOWN_PRESSED: sp1.move(0, 5); break; } sp1.paint(gra); Road.paint(gra); Wall.paint(gra); this.flushGraphics(); try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }