Forum Topic: As:performance & Optimisation Tips

(2,846 views • 27 replies)

This topic is 1 page long.

<< < > >>
None

T-H

Reply To Post Reply & Quote

Posted at: 6/30/05 11:15 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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!


Happy

Toast

Reply To Post Reply & Quote

Posted at: 6/30/05 11:16 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

Some nice tips there!


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/30/05 11:22 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

T-H

Reply To Post Reply & Quote

Posted at: 6/30/05 11:22 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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 :)


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 11:23 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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,


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 7/3/05 02:58 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

BBS Signature

None

The-Mercenary

Reply To Post Reply & Quote

Posted at: 7/3/05 05:01 PM

The-Mercenary EVIL LEVEL 17

Sign-Up: 01/22/03

Posts: 2,620

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?


None

WickedBiscuits

Reply To Post Reply & Quote

Posted at: 10/26/05 08:54 AM

WickedBiscuits NEUTRAL LEVEL 16

Sign-Up: 04/16/05

Posts: 150

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


None

IWantSomeCookies

Reply To Post Reply & Quote

Posted at: 10/26/05 08:59 AM

IWantSomeCookies LIGHT LEVEL 13

Sign-Up: 08/20/04

Posts: 3,295

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


None

Paranoia

Reply To Post Reply & Quote

Posted at: 10/26/05 09:00 AM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,696

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

None

Devenger

Reply To Post Reply & Quote

Posted at: 10/26/05 09:03 AM

Devenger NEUTRAL LEVEL 09

Sign-Up: 12/24/04

Posts: 1,103

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.


None

Rammer

Reply To Post Reply & Quote

Posted at: 10/26/05 09:21 AM

Rammer DARK LEVEL 32

Sign-Up: 06/08/03

Posts: 4,331

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


None

JCesta

Reply To Post Reply & Quote

Posted at: 11/1/05 05:49 PM

JCesta LIGHT LEVEL 09

Sign-Up: 07/08/05

Posts: 433

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


None

ColdLogic

Reply To Post Reply & Quote

Posted at: 11/1/05 06:02 PM

ColdLogic EVIL LEVEL 19

Sign-Up: 11/12/03

Posts: 524

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.


None

T-H

Reply To Post Reply & Quote

Posted at: 11/1/05 06:09 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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?


None

caseyo

Reply To Post Reply & Quote

Posted at: 11/1/05 07:17 PM

caseyo DARK LEVEL 23

Sign-Up: 05/14/05

Posts: 4,924

Cool, I'll have to use some of those.


None

ColdLogic

Reply To Post Reply & Quote

Posted at: 11/1/05 09:30 PM

ColdLogic EVIL LEVEL 19

Sign-Up: 11/12/03

Posts: 524

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


None

Nemo

Reply To Post Reply & Quote

Posted at: 11/1/05 09:51 PM

Nemo LIGHT LEVEL 34

Sign-Up: 06/13/03

Posts: 1,896

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


None

iiREDii

Reply To Post Reply & Quote

Posted at: 1/30/06 10:15 AM

iiREDii NEUTRAL LEVEL 05

Sign-Up: 02/12/04

Posts: 165

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...


None

Rantzien

Reply To Post Reply & Quote

Posted at: 1/30/06 10:38 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

suppi

Reply To Post Reply & Quote

Posted at: 3/25/07 09:49 PM

suppi LIGHT LEVEL 07

Sign-Up: 06/29/03

Posts: 119

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.


None

MaxManning

Reply To Post Reply & Quote

Posted at: 3/25/07 10:10 PM

MaxManning EVIL LEVEL 17

Sign-Up: 10/22/06

Posts: 87

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.


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 3/25/07 10:10 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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

Some times my "L" key decides not to work.


None

MaxManning

Reply To Post Reply & Quote

Posted at: 3/25/07 10:17 PM

MaxManning EVIL LEVEL 17

Sign-Up: 10/22/06

Posts: 87

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.


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 3/27/07 09:58 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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

Some times my "L" key decides not to work.


None

authorblues

Reply To Post Reply & Quote

Posted at: 3/27/07 11:04 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 3/31/07 01:40 AM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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

Some times my "L" key decides not to work.


None

Newsdee

Reply To Post Reply & Quote

Posted at: 3/31/07 06:41 AM

Newsdee LIGHT LEVEL 18

Sign-Up: 01/21/05

Posts: 650

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();


All times are Eastern Standard Time (GMT -5) | Current Time: 08:11 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!