2013-03-22 143 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."); 
    } 
} 

而且只使用大寫字符類的名字。