游戏生成滞后

时间:2013-03-22 15:18:30

标签: android android-asynctask render

我有这个游戏,它真的需要一个项目生成的负载,所以我把它全部都在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;
        }
    }

1 个答案:

答案 0 :(得分: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.");
    }
}

仅对类名使用大写字符。