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?