2013-03-22 106 views
0

我有这个游戏,它确实需要一个项目生成的负载,所以我把它全部在AsyncTask中处理,但是这个类有时需要很长的时间来执行,而其他时间则需要几个毫秒。什么原因导致这种情况有时会很快而且有时很慢?游戏生成滞后

public class handleStuff extends AsyncTask<String, Integer, String> 
    { 
     @Override 
     protected String doInBackground(String... arg0) { 
      Random r = new Random(); 
      if(r.nextInt(200) == 0 && clouds.size() <= 6) 
      { 
       Cloud c = new Cloud(SCREEN_WIDTH, SCREEN_HEIGHT, cloudBM1.getWidth(), cloudBM2.getHeight()); 
       clouds.add(c); 
      } 
      for(int i = clouds.size() - 1; i > -1; i--) 
      { 
       Cloud tempC = clouds.get(i); 
       if(tempC.x <= 0 - cloudBM1.getWidth() && tempC.dir == 0) 
        clouds.remove(i); 
       if(tempC.x >= SCREEN_WIDTH && tempC.dir == 1) 
        clouds.remove(i); 
      } 
      if(System.currentTimeMillis() - lastESpawn >= 750) 
      { 
       Enemy x = new Enemy(SCREEN_WIDTH, SCREEN_HEIGHT, enemy.getWidth(), enemy.getHeight()); 
       enemies.add(x); 

       lastESpawn = System.currentTimeMillis(); 
      } 
      for(int i = enemies.size() - 1; i > -1; i--) 
      { 
       Enemy tempE = enemies.get(i); 
       if(tempE.x <= 0 - enemy.getWidth()) 
        enemies.remove(tempE); 
      } 
      for(int i = 0; i < enemies.size(); i++) 
      { 
       Enemy tempE = enemies.get(i); 
       if(System.currentTimeMillis() - tempE.lastBulletSpawn >= 800 && tempE.x <= SCREEN_WIDTH) 
       { 
        EnemyBullet eb = new EnemyBullet(tempE.x, tempE.y, enemyBullet.getWidth(), enemyBullet.getHeight(), enemy.getWidth(), enemy.getHeight()); 
        enemyBullets.add(eb); 

        tempE.lastBulletSpawn = System.currentTimeMillis(); 
       } 
      } 
      for(int i = bullets.size() - 1; i > -1; i--) 
      { 
       Bullet tempB = bullets.get(i); 
       if(tempB.x >= SCREEN_WIDTH) 
        bullets.remove(i); 
      } 
      for(int i = enemyBullets.size() - 1; i > -1; i--) 
      { 
       EnemyBullet tempEB = enemyBullets.get(i); 
       if(tempEB.x <= 0 - enemyBullet.getWidth()) 
        enemyBullets.remove(i); 
      } 
      if(System.currentTimeMillis() - lastPBSpawn >= 400) 
      { 
       Bullet b = new Bullet(x, y, player.getWidth(), player.getHeight()); 
       bullets.add(b); 

       lastPBSpawn = System.currentTimeMillis(); 
      } 
      for(int i = explosions.size() - 1; i > -1; i--) 
      { 
       Explosion tempEx = explosions.get(i); 
       tempEx.update(); 
       if(tempEx.duration <= 0) 
        explosions.remove(i); 
      } 
      initial_load = true; 
      return null; 
     } 
    } 

回答

0

那么,没有人喜欢调试,我敢打赌,没有人会自己做测试。那取决于你。所以你所做的是记录一个操作需要多长时间。这很容易。一些示例代码。

public class HandleStuffJob extends AsyncTask<String, Integer, String> { 
    private long mTime; 

    @Override 
    protected String doInBackground(String... params) { 
     startLogging(); 

     try { 
      doSomeFancyStuff(); 
     } 

     catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     stopLogging(); 

     return null; 
    } 

    private void startLogging() { 
     mTime = System.currentTimeMillis(); 
    } 

    private void doSomeFancyStuff() throws InterruptedException { 
     /* 
     * For this demonstration I will just sleep the thread and pretend it to 
     * do some work. 
     */ 

     Thread.sleep(1234); 
    } 

    private void stopLogging() { 
     long now = System.currentTimeMillis(); 

     Log.d("Test", "The operation took " + (now - mTime) + " milliseconds or " + ((now - mTime)/1000) + " seconds."); 
    } 
} 

而且只使用大写字符类的名字。