Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-12-2010, 03:44 PM   #1
Registered User
Join Date: Mar 2009
Posts: 3
Default [AS2] More efficient enemy coding?

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.

Cardyak is offline   Reply With Quote
Old 12-13-2010, 08:25 PM   #2
throw a trace() in there
Join Date: Dec 2006
Posts: 1,982

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.
rrh is offline   Reply With Quote
Old 12-16-2010, 08:43 AM   #3
Registered User
Join Date: Dec 2010
Posts: 9

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.
epsilon2205 is offline   Reply With Quote
Old 12-18-2010, 07:35 PM   #4
Registered User
Join Date: Mar 2009
Posts: 3

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!
Cardyak is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 09:24 AM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.