PDA

View Full Version : Tips for reducing lag


EightySeven
02-18-2009, 01:47 AM
I was just wondering if we could compile a list of techniques to reduce lag. I know this would be of personal help since most of my fla files lag when too much happens.

bluemagica
02-18-2009, 04:18 AM
1-infinity) Don't make flash do too much calculation.

that's the only thing you do to reduce lag, how you do it is upto you!

a) Slice up your game world into smaller portions, so that there are less calculations to deal with, especially for collision detection!

b) a single forloop is better than putting onEnterFrame in 50 movieclips

c) Make sure your recursive functions and loops won't get stuck in infinite loops!

d) Learn to use bitmapData and reuse a single mc, rather than putting in a hundred mc to get a certain effect!

e) Always help flash mark stuff for garbage collection, just removing them from display list won't free up your pc resources!

f) use for..in/each loops over for loops, it will be faster and better!

kkbbcute
02-18-2009, 05:30 AM
1-infinity) Don't make flash do too much calculation.

That's retarded, its like someone asking you how to make sure that his car performs better, and you tell him to not use his car for driving, lol.

cjx3711
02-18-2009, 06:48 AM
bluemagica is right, he said do not let flash do too many calculations, not do not use flash to calculate anything. Another thing to reduce lag.

export the different parts as seperate swfs and use one main swf to put everything together to have the final product.

Do not set an alpha channel for objects if it is not nessesary.

For vector drawings, you may want to use optimise usually Ctrl + Alt + Shift + C. It can also be found in Modify > Shape > Optimise...
This will reduce tthe number of curves in thhe vector shape. Do not set the strength too high or the drawing may lose it's shape altogether

Flash Gordon
02-18-2009, 08:02 AM
That's retarded, its like someone asking you how to make sure that his car performs better, and you tell him to not use his car for driving, lol.

No...case in point. It is faster for Flash to use a hash table to look up complex values than having to calculate then on the fly as needed.

kkbbcute
02-18-2009, 09:15 AM
No...case in point. It is faster for Flash to use a hash table to look up complex values than having to calculate then on the fly as needed.

I know that, but what I'm saying is that for a newbie or something, the first sentence is quite useless, cuz they will be like going, how do I reduce the number of calculations, so actually the first sentence can be left out, but hey, I never said that the other points that he put down were invalid, did I?

bluemagica
02-18-2009, 09:31 AM
q(-_-)p Come on! Don't start a flame war on this!

The first point i said is precisely what you should be aiming to do! Lowering overall calculations( whether at the programming level or at the machine execution level) is the ultimate goal to reduce lag! There can be noone who knows all the ways to do it, if i write a hundred ways, someone else will come up with hundred+one way.....as a programmer or a game developer, we work with logic and concept, and we implement it our own ways!

kkbbcute
02-18-2009, 01:54 PM
Of course I agree, and I'm not flaming, just saying that Flash and in fact any program only lags when doing too many calculations for your computer to handle. Thus its like a logical fallacy when you tell others to reduce the number of calculations, because thats the only thing we can do anyways.

Oh yeah, a personal method that I use in games, tile the environments and use ._visible property instead of ._alpha = 0.

EightySeven
02-18-2009, 02:17 PM
I have a question on colission detection. If I had my MCs only check against objects within x pixils of itself would that reduce lag or keep it the same because of the extra calculations?

And how do you mark things for garbage collection?

bluemagica
02-18-2009, 03:07 PM
1) that will reduce lag as long as you don't go about looping through all available mc's everytime to see if they are in range!

2) Just make sure you remove all the references to the object! meaning remove it from display list, any arrays it may be stored on, etc!

rrh
02-18-2009, 04:03 PM
If you are using AS3, then give all your variables explicit types.
var frank:int = 1;
not
var frank = 1;

If you are using AS2, then start using AS3.

EightySeven
02-18-2009, 06:02 PM
I use AS3 =) and i give variables types, except static const because, well, why

rrh
02-18-2009, 06:12 PM
Hmm. I don't really know how typing of constants is handled.

newblack
02-18-2009, 06:42 PM
1) that will reduce lag as long as you don't go about looping through all available mc's everytime to see if they are in range!
this is completely dependent on the type of collision detection you're using. in fact, excluding the need for a more intensive and accurate test is a whole sub-field of collision detection called broad or coarse (phase) collision detection. so yes, doing a quick range test will reduce the amount of computation as it is about as simple as it gets (next to AABB tests). remember to avoid square root calls whenever necessary, so a range overlap would look like this:

const MAX_RANGE_SQ:Number = 100 * 100; //100 is max range
var dx:Number = someObject.x - anotherObject.x;
var dy:Number = someObject.y - anotherObject.y;
var isInRange:Boolean = dx * dx + dy * dy <= MAX_RANGE_SQ;

Other tips:

Exit algorithms as soon as possible
Use a sine/cosine lookup table instead of calculating at runtime
Pool objects- instantiation and garbage collection are computationally intensive tasks
Rendering will always be the biggest generic bottleneck in Flash- don't be doing anything you don't have to, and read up on methods that require the least overhead.

newblack
02-18-2009, 06:44 PM
I use AS3 =) and i give variables types, except static const because, well, why
Seriously?

EightySeven
02-20-2009, 06:22 PM
Seriously?

of course, why use an Object when I know its going to be an int or why use an int when i can use a uint cut that stuff down to as simple as u need it. sometimes Objects are necessary as some of my classes need to function with any kind of input, or any kind of display object

newblack
02-20-2009, 07:14 PM
i have no idea what you mean.

rrh
02-20-2009, 07:59 PM
Weird, so I just ran some tests for constants.

var frank:int;
var joe:String;
var then:Number = getTimer();

for (frank=Consts.starter;frank<Consts.ender;++frank) {
joe=Consts.north;
}

var now:Number = getTimer() - then;
trace (now);

I couldn't find any difference between the static variables having defined types or not. That was giving me results in the low 1400's whichever I used.

Local consts, however, were a lot faster than static consts. This is giving me about high 200's:
var frank:int;
var joe:String;
var then:Number = getTimer();
const starter = Consts.starter;
const ender = Consts.ender;
const north = Consts.north;

/*for (frank=Consts.starter;frank<Consts.ender;++frank) {
joe=Consts.north;
}*/

for (frank=starter;frank<ender;++frank) {
joe=north;
}

var now:Number = getTimer();
trace (now);

And typing the local consts dropped it down to low 100's:
var frank:int;
var joe:String;
var then:Number = getTimer();
const starter:int = Consts.starter;
const ender:int = Consts.ender;
const north:String = Consts.north;

/*for (frank=Consts.starter;frank<Consts.ender;++frank) {
joe=Consts.north;
}*/

for (frank=starter;frank<ender;++frank) {
joe=north;
}

var now:Number = getTimer();
trace (now);

So before, I had assumed static consts would be treated the same as local consts, but now I realize they must be referencing the class name to access the value.

EightySeven
02-23-2009, 07:06 PM
just an update on my lag issues, i went from about 20 enemies on screen to around 60 before the lag kicks in. so this thread helped me work out some bugs thanks a bunch guys! thought I couldn't incorporate all of your suggestions on this issues because of time restraints but they will be tested in future applications for sure!