日期:2014-05-20  浏览次数:20681 次

疯了!让这个Sprite实例运行起来!
Sprite没用过、那些API也看不懂、所以在网上找了一个实例、编译没有报错、但事实上是运行不起来的。
      烦请高手帮我修改修改,做成ME程序 <即包含MIDlet的程序> 要确保复制上去不用修改就可以直接运行。
    先鞠个弓。
完整程序如下:
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)