Be a Supporter!

As:performance & Optimisation Tips

  • 3,813 Views
  • 27 Replies
New Topic Respond to this Topic
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
As:performance & Optimisation Tips 2005-06-30 11:15:22 Reply

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!

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As:performance & Optimisation Tips 2005-06-30 11:16:58 Reply

Some nice tips there!


BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As:performance & Optimisation Tips 2005-06-30 11:22:10 Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As:performance & Optimisation Tips 2005-06-30 11:22:51 Reply

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

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As:performance & Optimisation Tips 2005-06-30 11:23:08 Reply

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,

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to As:performance & Optimisation Tips 2005-07-03 14:58:08 Reply

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


BBS Signature
The-Mercenary
The-Mercenary
  • Member since: Jan. 22, 2003
  • Offline.
Forum Stats
Member
Level 18
Musician
Response to As:performance & Optimisation Tips 2005-07-03 17:01:27 Reply

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?

WickedBiscuits
WickedBiscuits
  • Member since: Apr. 16, 2005
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to As:performance & Optimisation Tips 2005-10-26 08:54:17 Reply

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

IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to As:performance & Optimisation Tips 2005-10-26 08:59:21 Reply

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

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to As:performance & Optimisation Tips 2005-10-26 09:00:35 Reply

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
Devenger
Devenger
  • Member since: Dec. 24, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As:performance & Optimisation Tips 2005-10-26 09:03:32 Reply

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.

Rammer
Rammer
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 33
Programmer
Response to As:performance & Optimisation Tips 2005-10-26 09:21:08 Reply

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

JCesta
JCesta
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As:performance & Optimisation Tips 2005-11-01 17:49:07 Reply

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

ColdLogic
ColdLogic
  • Member since: Nov. 12, 2003
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As:performance & Optimisation Tips 2005-11-01 18:02:16 Reply

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.

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As:performance & Optimisation Tips 2005-11-01 18:09:54 Reply

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?

caseyo
caseyo
  • Member since: May. 14, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to As:performance & Optimisation Tips 2005-11-01 19:17:01 Reply

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

ColdLogic
ColdLogic
  • Member since: Nov. 12, 2003
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As:performance & Optimisation Tips 2005-11-01 21:30:28 Reply

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

Nemo
Nemo
  • Member since: Jun. 13, 2003
  • Offline.
Forum Stats
Member
Level 34
Game Developer
Response to As:performance & Optimisation Tips 2005-11-01 21:51:23 Reply

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
iiREDii
iiREDii
  • Member since: Feb. 12, 2004
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As:performance & Optimisation Tips 2006-01-30 10:15:54 Reply

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

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As:performance & Optimisation Tips 2006-01-30 10:38:54 Reply

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
suppi
suppi
  • Member since: Jun. 29, 2003
  • Offline.
Forum Stats
Member
Level 07
Audiophile
Response to As:performance & Optimisation Tips 2007-03-25 21:49:25 Reply

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.

MaxManning
MaxManning
  • Member since: Oct. 22, 2006
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As:performance & Optimisation Tips 2007-03-25 22:10:32 Reply

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.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to As:performance & Optimisation Tips 2007-03-25 22:10:55 Reply

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

MaxManning
MaxManning
  • Member since: Oct. 22, 2006
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As:performance & Optimisation Tips 2007-03-25 22:17:08 Reply

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.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to As:performance & Optimisation Tips 2007-03-27 21:58:02 Reply

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

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As:performance & Optimisation Tips 2007-03-27 23:04:20 Reply

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
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to As:performance & Optimisation Tips 2007-03-31 01:40:21 Reply

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

Newsdee
Newsdee
  • Member since: Jan. 21, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As:performance & Optimisation Tips 2007-03-31 06:41:42 Reply

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