PDA

View Full Version : [AS2] "For" loops slowing down game


o100011e10010
09-13-2009, 12:36 AM
Hey guys,

So i've been working on a game and i've been implementing for loops here and there, causing my game to slow down dramatically. The player adds objects onto the screen by mouse clicks and the main character in the game must test for hitTests for all of those objects. So I have a counter ("k") set up and each new object that is added has an instance name of "hitBox"+k and there's a for loop that tests (m=0;m<k;m++) and it tests hitTests that way.

Like this: (this actionscript is on the main character)

for (m=0; m<=_global.k; m++) {
if (_root["hitBox"+m].hitTest(_x, _y, true)) {
test1 = true;
}
}


Is there a better way of doing this so that after like 15 are added it doesn't slow down very much?

thnx,
-o100011e10010

rrh
09-13-2009, 06:16 AM
Instead of referencing them all with _root["hitBox"+m] fill an array with all the instances and then loop through the array.

And possibly instead of hitTesting with True for everything, try testing them with false and then retesting with true if the first test is true.

You can get even faster if you use AS3, thanks to variable casting.

sam.uk.net
09-13-2009, 03:27 PM
Hi,

Please use AS tags when posting Actionscript code.

Just a little tip with the loop.

instead of using

m++


use


m+=1


I read somewhere that it processes faster using the += operator, the difference might not be noticeable but it should generally speed things up.

Hope that helps!

sam.uk.net

o100011e10010
09-13-2009, 04:31 PM
wow, thnx for the replies and great tips, imma try them now

thank you,
-o100011e10010

sam.uk.net
09-13-2009, 09:12 PM
No problem! ;)

sam.uk.net