首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

高手来!Sprite的使用没有运行成功,好像是路径有关问题

2011-12-27 
高手来!Sprite的使用没有运行成功,好像是路径问题。我把ai.png存在了WTK的res下、也用eclipse试过,直接放在

高手来!Sprite的使用没有运行成功,好像是路径问题。
我把ai.png存在了WTK的res下、也用eclipse试过,直接放在目录下。但运行结果都是 "路径不对 "
我没有用过Sprite请大家帮帮忙,找找问题,帮我修改好。
完整代码如下:


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( "/ai.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();
        }
}

[解决办法]
按你的代码,应该把ai.png放到java文件一个目录下.

热点排行