0

Hey guys so i am creating a game and when i test it in Adobe Scout is states that this function is causing it to go over its budget by 600% checkMainGunMissleHitZapper();

So i was wondering if their was a way to optimize it. It is a double for loop since the missle is an array and the enemy is also handled by an array.

Whenever the hittest takes place between the 2 objects the screen just completely pauses and slows down the performance majorly on a mobile device.

Here is how i set up the loops:

private function checkMainGunMissleHitZapper():void 
    {
        //loop through al current missles
        for (var i:int = 0; i < aMainGunMissleArray.length; i++)
        {
            //get current missle in loop
            var currenMainMissle:mcMainGunMissle = aMainGunMissleArray[i];

            //loop through all our enemies
            for (var j:int = 0; j < aEnemyZapperArray.length; j++)
            {
                //get current enemy in j loop
                var currentZapper:mcEnemyZapper = aEnemyZapperArray[j];

                //test if current missle is hitting current enemy
                if (currenMainMissle.hitTestObject(currentZapper))
                {
                    //create an explosion

                    //create a new explosion instance/movieclip
                    var newZapperExplosion:mcEnemyZapperExplosion = new mcEnemyZapperExplosion()
                    //add our explosion to the stage
                    stage.addChild(newZapperExplosion)
                    //position explosion to enemy
                    newZapperExplosion.x = currentZapper.x
                    newZapperExplosion.y = currentZapper.y


                    //remove missle
                    currenMainMissle.destroyMainGunMissle()
                    //remove missle from missle array
                    aMainGunMissleArray.splice(i, 1);

                    //remove enemy on stage
                    currentZapper.destroyEnemyZapper()
                    //remove enemy from array
                    aEnemyZapperArray.splice(j, 1);

                    nScore += 20;
                    updateScoreText();


                }

            }
        }
    }

Some forums i read were saying that this would optimize the code:

for (int k = 0; k < N * N; ++k) { int i = k / N; int j = k % N; }

But i wouldnt even know where to begin with that. kind of past my as3 skill level.

But can you see any way to optimize these for loops?

4

1 回答 1

0

I think the warning is due to the hitTestObject call within the inner loop:

if (currenMainMissle.hitTestObject(currentZapper))

hitTestObject(...) is a relatively slow process, so if you are looping through many hit tests, it can take a long time - hence the warning from Scout.

Optimising the loops (such as in the way mentioned in forums after your code) will make a negligible difference - 99% of the time spent in the loop will be due to hitTestObject.

The 'trick' is to reduce the number of hitTestObject calls you make - such as checking whether x and y values of the 2 objects are within a certain range - if theyre not, no need to check for a collision.

Or when there is a collision (and missile is removed), no need to continue looping through further checks, so after this line:

updateScoreText();

Add this line:

break;

This will cause inner loop to end (and next outer loop to begin)

Optimising collision detection is a vast subject and there are many techniques so i cant give you a specific solution for your case without knowing the details of your game - googling "actionscript collision detection" should provide a few tutorials.

于 2013-04-14T22:23:09.283 回答