View Full Version : [AS2] More efficient enemy coding?

12-12-2010, 03:44 PM
Hello guys, I'm fairly new here.

I'm developing a small survival horror game, everything seems to work dandy, apart from major lag. I think I've traced this down to the fact the code for the enemies hit detection and physics, is in the enemy itself. So when I duplicate about 50 enemies for a high level, it obviously runs in each enemy instance.

Is there a way to use a for loop or function to run through all the enemies as and when they spawn? I've tried using onLoad/onEnterFrame without much success. I can post the code if need be.

I would very much appreciate some help. :)


12-13-2010, 08:25 PM
When you create the enemies, put them in an array. Then loop through the array.

Though also post the content of what you're doing to the enemies to see if there's anything obvious that can be improved if it's being run 50 times, no matter how it's run.

12-16-2010, 08:43 AM
Afew tricks you need to know:

1. Create your enermy: mcEnemy = new Enemy();

2. Place it into an Array: enemies[] by pushing it into th the Array:
2. The perform the hit test against the elements in the aray including itself as you traverse the array enemies:
for (var i:Number=0; i<enemies.length; i++)
if (enemy != enemies[i] && enemy.hitTestObject(enemies[i]))
// goto and Play . . .
3. Remove the hit current enemies: enemies.pop();
4. Remove the hit enemies in the array: enemies.splice(i,1);
5. if it is not enemies hitting enemies with the array then amend the if statement in 2 above.

All the above is to be written with the onEnterFramehandler function/method.
Hope this helps.

12-18-2010, 07:35 PM
I managed to find a remedy myself, a for loop that looks up all the enemies somewhere else in the file.

Thank you Epsilon for the tips, I will use that method in the future! :)