Home » ActionScript 2.0, ActionScript 3.0, Flex

Efficient AS3 Programming Practices

10 August 2010 No Comment
Share on Facebook

Some times our application may consume much memory or resource in runtime, There are ways to control the memory usage or CPU utilization of the application by efficient use of code.

Some of Flash, as3, actionscript Tips and Tricks

1. While creating new Array or Object.

Never use
new Array();
new Object();

Always use
var object:Object = {};
var arr:Array = [];

2. Dont use too many dot syntax.

Never use
outcontainer.inner2.inner1.inner.x = 10;
outcontainer.inner2.inner1.inner.width = 100;

Always use
var spr:Sprite = outcontainer.inner2.inner1.inner as Sprite
spr.x = 10;
spr.width = 100;

Note: Idea is to hold reference of the Object

3. Use int in for loop

for( var count:int=0; count<10;count++){}

4. Hold the length reference in a variable before for loop.

var len:Number = arr.length();
for( var count:int=0; count

5. To empty an array.

use arr = []; or arr.length = 0

6. Never use setStyles() in Flex. Use of this method makes calls to Framework.

7. Use XOR operator to swap variables without usage of temporary variable.

Never use
var a:Number = 10;
var b:Number = 20;
var temp:Number;
temp = b;
b = a;
a = temp;

Always use
var a:Number = 10;
var b:Number = 20;
a = a ^b;
b = a^b;
a = a^b;

8. To declare two or more variable of same type.

Never use
var a:Number;
var b:Number;
var c:Number;

Always use
var a:int,b:int,c:int;

More in next post… Happy coding :D


We don't share your email anywhere, grab our rss via feedburner

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.