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

一个多星期拉还没解决的游戏问题!请高手来!急。。。。。。。。。。。。。
目前做拉个飞机飞行的小游戏
在电脑上运行没问题,在真机上就出问题
真机上运行会出现严重延迟迟缓现象,使游戏很不流畅!
飞机是个精灵类,在线程中根据输入来控制飞机运行   delay=20
代码如下:

private   void   paintBuffer()//画缓冲区
  {
            buffer=Image.createImage(this.getWidth(),this.getHeight());
            Graphics   g=buffer.getGraphics();
            g.drawImage(this.backgroudimg,   0,   0,g.TOP|g.LEFT);   //绘制背景
            plane.DrawPlane(g);//绘制飞机
  }
  private   void   drawScreen(Graphics   g)   {
 
  g.setColor(0xffffff);
          g.fillRect(0,   0,   getWidth(),   getHeight());
          g.setColor(0x0000ff);
         
          g.drawImage(this.buffer,   0,   0,g.TOP|g.LEFT);
 
          flushGraphics();
}
public   void   run()   {//主线程
          Graphics   g   =   getGraphics();
          long   newtime;
          int   alltime=0  
          while   (isPlay)   {
          alltime=   alltime+Integer.parseInt(new   Long(delay).toString());
          newtime=System.currentTimeMillis();
                  this.drawScreen(g);
                  g.setColor(0xffffff);
                  flushGraphics();
                    Planeinput();//接收键盘输入控制方向
                  this.paintBuffer();
                  newtime=System.currentTimeMillis()-newtime;
                  try{
                          if   (newtime <this.delay)
        {
        Thread.currentThread().sleep(delay-newtime);
        }
                        }
                  catch   (InterruptedException   ie)   {}
          }
     
  }


手机配置:
Java扩展   MIDP   2.0环境  
内存容量   16MB总存储空间。包括3.2MB用户可用存储空间  
 


------解决方案--------------------
将paintBuffer方法中的
buffer=Image.createImage(this.getWidth(),this.getHeight());
放到构造函数中,
buffer不要每一帧都创建一次。

要知道,创建Image是非常消耗系统资源的操作,会导致程序变的很慢。
------解决方案--------------------
buffer=Image.createImage(this.getWidth(),this.getHeight());
Graphics g=buffer.getGraphics();
g.drawImage(this.backgroudimg, 0, 0,g.TOP|g.LEFT); //绘制背景

画背景的可以1次放到外面去画 不能每次循环都画背景。如果是个长横画布游戏可以做个判断如果飞机飞行超过当前屏幕宽度3/4的时候再绘制下面的背景(并且要把以前的超出屏幕背景K.O掉),
delay可以调小点,减少线程休眠时间
if (newtime <this.delay)
{
Thread.currentThread().sleep(delay-newtime);