00:00
00:00
Newgrounds Background Image Theme

AllHailInsomnia just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As:performance & Optimisation Tips

5,398 Views | 26 Replies
New Topic Respond to this Topic

AS:Main
Just a few general tips I find extremely helpful to keep filesize down and runtime speed up in games and movies.

The .fla file bloat
Every time you File -> Save in Flash, the un necessary data you may have deleted from the stage or library is not removed. See it for yourself by importing a large media file, saving, then re opening and deleting it from the library before saving again. The filesize will still remain gigantic. Flash tries to keep saving time and memory use down by not removeing the waste.

I'll state now that this file bloat does not affect your .swf in any way. This fix is to merely reduce the hard drive space taken by your .fla ; and reduce the time taken when loading it up.

Flash MX2004 has the File -> Save & Compact option, which can be used to remove this disregarded data when saving. But in versions prior to MX2004, this optons is not avalaible.

For the many users here that prefer Flash MX and even ver.5(God knows why), the fix is to File -> Save as, and save under a different name. This has the same effect as File -> Save & Compact (in MX2004).

Anti-Aliasing
Anti-aliasing is a technique performed by Flash to try and smooth out jagged vector curves. It is rather processor intensive, and can cause significant slowdown (in higher framerate games and movies) if certain techniques are not followed.

Anti-aliasing is needed more when the contrasing colours appear adjacent to eachother. You can save on memory use by trying to make curved lines a similar colour to the background where possible.

Anti aliasing can be turned off by switching the movie's quality to "LOW". This is where the tradeoff occurs between running speed and rendering quality. When running at low quality, I would suggest several ways to hide the jagged curves that will appear.

-Again, use non-contrasting colours , so the jaggies will blend in.
-Try to use horizintal and vertical graphics wherever possible, so less jagges edges will be present.
-Have fast moving shapes, the edges are a lot less noticable
-An interesting one here - use pixel fonts, such as old NES and Atari style ones, these look pretty much exactly the same in low quality.

Bitmaps / Vectors/Graphics
First of all I will say Always try to avoid reducing alpha on bitmaps. Now this is a huge performance hinder on all movies, rendering transleucent or transparent bitmaps (_alpha < 100) is a lot of work, even more so if they are moving.

Using vectors and bitmaps:
Bitmaps can move a lot faster than vector graphics in flash. When there is little screen activity, this statement is not true, but when there are lots of objects (e.g. characters) moving accross the screen, bitmap artwork would be a better options, as it saves flash having to re define and re draw many co ordinate points each frame.

For bitmap sprite graphics, .PNG files have been highly recommended for rendering speed on stage.

Another point worth covering would be offstage content. In .swf's, if objects such as characters and enemies are positioned offstage, set ready to be brought in, or after being sent off (etc.) Creating an empty frame inside the movie clips themselves, and sommanding the mc to gotoAndStop on this frame while they are offstage can save on speed.

The offstage content is still rendered by Flash, even if it is not visible to the user. Having a blank frame is an excellent idea for RTS(realtime strategy) style games (command & conquor style), or any others where the user can scroll to a different area of the map, leaving enemies and players offstage. -Could also be implemented in movies for scrolling backgrounds and such.

Actionscript Optimisation
Just a few key points here, generally, a tradeoff occurs between clean, reusable code, and efficient faster running code.

Try to minimise the amount of different onEnterFrame handlers in the game/movie
Using too many of these handlers reduces run speed. Where possible, apply the required actions inside ONE of these handlers that can run through the entire timeline, instead of using many.

I've amazed myself at the amount of times I can substitute an onMouseMove handler instead of an onEnterFrame for things like cursors and scrolling shooter games, not only does mouseMove only apply when actually necessary (when the mouse moves), it gives a smoother feel (so far I have not noticed any slowdown despite the higher frequency).

Using loops
It is apparently believed that
"A typical game loop where you attach movieclips to a target mc.
Using the while loop instead of for results in better performance"

-Taken from gotoandplay.it

I'll note that the while loop is proven to be the faster running loop, as it takes less information in, I would still reccomend the for loop, unless you are really desperate for performance.

Multiple var Declaration
You can use this if you are really squeezing, people running websites with flash interfaces have given this method, I wouldn't really say it is of great use to movie.game developers.

Intead of:
a=0;
b=0;
c=0;
d=1;
e=1;

You can use:
a=b=c=0;
d=e=1;

Always declare local variable in functions
Always decare your local variables using var. Making sure the variables used inside your functions are local makes them easier to access by the flash player, resulting in faster running speed. If you declare variables using name = ;, they will be declared globally, and will take up more of the movie's virtual memory.

Accessing Local Variables
When accessing local variables, using the with operator as opposed to repeatedly using this. saves on typing & CPU usage.

e.g Using:
with(this){
a = 1
b = 2
c = 3
}

Instead of:
this.a =1
this.b = 2
this.c = 3

Key Optimisation
This is one i came accross and found interesting.
Using:
keyDown = Key.isDown;
keyLeft = Key.LEFT;
if (keyDown(keyLeft)){}

Instead of repeating:
if(Key.isDown(Key.LEFT)) ;

More on Binary Increasement

Hope there wern't too many spelling mistakes! Leave any feedback!

Response to As:performance & Optimisation Tips 2005-06-30 11:16:58


Some nice tips there!


BBS Signature

Response to As:performance & Optimisation Tips 2005-06-30 11:22:10


Great post. I particularly liked the AS optimisation, I wasn't aware of several of those. It's good to be learning new stuff =)


- - Flash - Music - Images - -

BBS Signature

Response to As:performance & Optimisation Tips 2005-06-30 11:22:51


At 6/30/05 11:22 AM, Denvish wrote: Great post. I particularly liked the AS optimisation, I wasn't aware of several of those. It's good to be learning new stuff =)

From the man himself! Greatly appreciated :)

Response to As:performance & Optimisation Tips 2005-06-30 11:23:08


Very useful though it doesn't really have to do as much with AS as with general flash tips.

although code really isn't much of the file size, it's still good to have it small and clean...

this could use some explenation about AS opritimization itself, like using shapeFlag hitTest instead of multiple hitTest loops, using macromedia functions over hand written code since they are programmed in C and not flash, and deleting variables that are out of use...

nice work

p.s.

if you could extend on AS runtime optimization too that would be nice,

Response to As:performance & Optimisation Tips 2005-07-03 14:58:08


cool, I never really knew about the "with(this)" trick


BBS Signature

Response to As:performance & Optimisation Tips 2005-07-03 17:01:27


I knew most of the stuff you posted, but some of the stuff can help me out. Its too late to implement that into my game now, but I'll optimize my code better. BTW I have a question, can someone tell me how much ram some things in flash use up? Would deleting variables when they arnt needed anymore help?

Response to As:performance & Optimisation Tips 2005-10-26 08:54:17


wow thanks alot!and yes MC's really do take up space but i like FBF better :/

Response to As:performance & Optimisation Tips 2005-10-26 08:59:21


At 7/3/05 02:58 PM, Afro_Ninja wrote: cool, I never really knew about the "with(this)" trick

Same here, you learnt something new every day huh!


"Actually, the server timed out trying to remove all your posts..."

-TomFulp

Response to As:performance & Optimisation Tips 2005-10-26 09:00:35


At 10/26/05 08:54 AM, Ryoku97948 wrote: wow thanks alot!and yes MC's really do take up space but i like FBF better :/

When I gave you the link to this it was to get help from, not to bump with your poorly structures sentances.

I hate people.


BBS Signature

Response to As:performance & Optimisation Tips 2005-10-26 09:03:32


Wow, I actually knew about it. But I didn't know aobut the Save and Compress, and I definitely didn't know about the Key Optimization.

Thanks a bunch. :D


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to As:performance & Optimisation Tips 2005-10-26 09:21:08


i thought "Save and Compact..." was just another way of saving the fla, and all it did was compress it.

i guess i was wrong :P


snyggys

Response to As:performance & Optimisation Tips 2005-11-01 17:49:07


a little you should know with flash 8 is that filters ALSO take up a lot of the performance just like alpha

Response to As:performance & Optimisation Tips 2005-11-01 18:02:16


well heres my AS, processors require many steps in order to divide, no mater what it is AS VB C++ avoid division as much as possible.. etc. 10/5 could be described as 10 * 0.5

multiplication require less steps in the processor.

Response to As:performance & Optimisation Tips 2005-11-01 18:09:54


At 11/1/05 06:02 PM, ColdLogic wrote: well heres my AS, processors require many steps in order to divide, no mater what it is AS VB C++ avoid division as much as possible.. etc. 10/5 could be described as 10 * 0.5

multiplication require less steps in the processor.

Really? Now thats an interesting one

I'm guess Math.sqrt() would be another intensive one?

Response to As:performance & Optimisation Tips 2005-11-01 21:30:28


I have no idea about the sqaure root function actually, kinda makes sense though, i think

Response to As:performance & Optimisation Tips 2005-11-01 21:51:23


This is more geared for Flash MX (I think), but it still has a lot of good tips for optimizing your Actionscript: http://www.oddhammer..rmance/old_index.htm


BBS Signature

Response to As:performance & Optimisation Tips 2006-01-30 10:15:54


So If I am gonna have multiple baddies on screen it is best to use bitmap graphics in place of vector? When you break (ctrl+B) drawings in flash do they remain vector based or do they become rasterized?

I am working on a game and need to know whether I am going the wrong route using vector graphics...

Response to As:performance & Optimisation Tips 2006-01-30 10:38:54


At 11/1/05 06:02 PM, ColdLogic wrote: 10/5 could be described as 10 * 0.5

Haha, now that's just silly. 10/5 = 2 while 10 * .5 = 5
But I know what you mean =P

At 1/30/06 10:15 AM, iiREDii wrote: So If I am gonna have multiple baddies on screen it is best to use bitmap graphics in place of vector?

Nah, I'd stick to vector.


BBS Signature

Response to As:performance & Optimisation Tips 2007-03-25 21:49:25


thanks it helped alot.

just a tip- my game was running really slow, i have it on automatic quality, so it went on low

immediatly and still ran really slow. so, i took the advice of this forum and converted the

background to a png and pasted it back into the flash. now it stays at a nice medium quality

and 22 or so fps (the pre set fps). i highly reccomend it, especially if you make games somewhat sloppily like i do.

Response to As:performance & Optimisation Tips 2007-03-25 22:10:32


At 11/1/05 05:49 PM, JCesta wrote: a little you should know with flash 8 is that filters ALSO take up a lot of the performance just like alpha

Yep, filters eat up performance, and it should also be noted that text does as well. If you put a text instance on the screen, it's rendering it each frame because the background of a text symbol is transparent. If the text is static and doesn't need to change (if it's not a score or something) , make it a movieclip with a solid background and bitmap cache it.

Response to As:performance & Optimisation Tips 2007-03-25 22:10:55


would multiple var declaration really help at all. have you done lag test because it shouldn't matter. and if it does it would only help the first frame.

and why wasn't datatyping on this list

Response to As:performance & Optimisation Tips 2007-03-25 22:17:08


Oh... sorry for the double post.

Scaling and rotation also take up performance.

Having a clip with say 100 frames for each scale level 1-100, is quicker than the movieclip having to re-scale each frame.

Likewise, a clip with 360 frames for rotation is quicker than rotating via the property.

Response to As:performance & Optimisation Tips 2007-03-27 21:58:02


At 3/25/07 10:17 PM, MaxManning wrote: Oh... sorry for the double post.

Scaling and rotation also take up performance.

Having a clip with say 100 frames for each scale level 1-100, is quicker than the movieclip having to re-scale each frame.

Likewise, a clip with 360 frames for rotation is quicker than rotating via the property.

but thats stupid and takes up more space. il never use that no matter how efficient it is

Response to As:performance & Optimisation Tips 2007-03-27 23:04:20


At 3/25/07 10:17 PM, MaxManning wrote: Scaling and rotation also take up performance.

im going to have to call bullshit on all of that.
flash renders a rotated movieclip in the same way actionscripted rotation does.

please provide evidence at such strange claims next time. especially if youre
going to suggest something that goes so obviously against common sense.


BBS Signature

Response to As:performance & Optimisation Tips 2007-03-31 01:40:21


At 3/27/07 11:04 PM, authorblues wrote:
At 3/25/07 10:17 PM, MaxManning wrote: Scaling and rotation also take up performance.
im going to have to call bullshit on all of that.
flash renders a rotated movieclip in the same way actionscripted rotation does.

please provide evidence at such strange claims next time. especially if youre
going to suggest something that goes so obviously against common sense.

phew i was worried that people weregonna start suggesting this insane method

Response to As:performance & Optimisation Tips 2007-03-31 06:41:42


I realize the original post is a bit old (back from 05?) but two of the AS optimization are simply not worth it with Flash 8. So I've made an experiment to benchmark both.

It turns out that using "while" and the keyDown trick have insignificant benefits (50 ms benefit for looping 100,000 times, which is insane anyway for a single frame). So the optimization is pretty useless, except maybe for an ego boost equivalent of putting a "Type R" sticker to a crappy old car.

Interestingly the results vary slightly at each run, but this may be due to how busy the CPU is (proving the test is quite sensitive, but then again we're talking about things that happen in less than a fraction of a second).

_Key.isDown vs. keyDown_
Key.isDown - elapsed: 600 ms.
isDown - elapsed: 550 ms.
isDown benefit: 9% - 50ms.

_While vs. For loops_
while loop - elapsed: 488 ms.
for loop - elapsed: 515 ms.
while loop benefit: 5% - 27ms.

Here's the code if somebody wants to play with it:
var max:Number = 100000;
trace("Running tests at " + max + " iterations.\n");
var startTime:Number;
var a:String = "";
var i:Number = 0;
var res1:Number;
var res2:Number;
trace("_Key.isDown vs. keyDown_");
startTime = getTimer();
for(i = 0;i<max;i++) {
if(Key.isDown(Key.LEFT)) {
a+="0";
}
}
endTime = getTimer();
res1 = endTime - startTime;
trace("Key.isDown - elapsed: " + res1 + " ms.");

a="";
var isDown = Key.isDown;
var keyLeft:Object = Key.LEFT;
startTime = getTimer();
for(var i:Number = 0;i<max;i++) {
if(isDown(keyLeft)) {
a+="0";
}
}
endTime = getTimer();
res2 = endTime - startTime;
trace("isDown - elapsed: " + res2 + " ms.");
trace("isDown benefit: " + Math.floor((res1/res2 - 1)*100) + "% - " + (res1-res2) + "ms.");

//test for while loops
trace("\n"+ "_While vs. For loops_");
a = "";
i=0;
startTime = getTimer();
while(i<max) {
a+="0";
i++;
}
endTime = getTimer();
res2 = (endTime - startTime);
trace("while loop - elapsed: " + res2 + " ms.");
a="";
startTime = getTimer();
for(i=0; i<max; i++) {
a+="0";
}
endTime = getTimer();
res1 = (endTime - startTime);
trace("for loop - elapsed: " + res1 + " ms.");

trace("while loop benefit: " + Math.floor((res1/res2 - 1)*100) + "% - " + (res1-res2) + "ms.");

stop();