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;
        }
    }
4

1 回答 1

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

并且仅使用大写字符作为类名。

于 2013-03-22T16:32:27.413 回答