Notice: register_sidebar_widget is deprecated since version 2.8! Use wp_register_sidebar_widget() instead. in /home/blogcv/public_html/wp-includes/functions.php on line 3201
Notes: General AS3 Tips | CourseVector Blog

Notes: General AS3 Tips

Wednesday, January 27th, 2010 | Web Development | Gabriel Mariani
This is the final bit migrated from the Labs Wiki. This is just a short list of quick and easy tips to keep in mind when programming AS3.

  • Do not use Objects, if you know which properties will be finally involved. Write a class for it; AS3 seems to register a memory space for them and will have faster access.
  • Keep variable names as short as possible. While this doesn’t help a lot, it does help a little.
  • Items of 0 alpha are still draw on the stage. Use visible = false to save CPU cycles

Reuse instantiated classes

Create constants for commonly used objects, such as new Point(0,0)

// 100k loops: 75ms
new Point(0,0);

// 100k loops: 8ms
private static const POINT:Point = new Point(0,0);
POINT;

Getters

Store getter properties as local variables when using them more than once in a method (such as .graphics, .transform)

// 100k loops: 202ms
somesprite.graphics.clear();
somesprite.graphics.beginFill(0x000000);
somesprite.graphics.drawRect(0,0,10,10);
somesprite.graphics.endFill();

// 100k loops: 22ms
var n:Graphics = sprite.graphics;
n.clear();
n.beginFill(0x000000);
n.drawRect(0,0,10,10);
n.endFill();

Class Reflection

Create custom reflection methods instead of using: getDefinitionByName(getQualifiedClassName(object))

package {
	public class SomeClass {
		public function get reflect():Class {
			return SomeClass;
		}
	}
}
var someObject:SomeClass = new SomeClass();
var someClass:Class;
var i:int;

// 66ms
for(i=0; i<100000; i++) {
	someclass = getDefinitionByName(getQualifiedClassName(someObject)) as Class;
}

// 28ms
for(i=0; i<100000; i++) {
	someclass = Object(someObject).constructor;
}

// 16ms
for(i=0; i<100000; i++) {
	someclass = someObject.reflect;
}

Constants from other classes

	// This takes 34.08ms to execute
	var tmpVar:int;
	for(var i:Number=0; i<100000; i++) {
		tmpVar = SomeClass.SOME_CONSTANT;
	}

	// This takes 15.8ms to execute
	var tmpVar:int;
	var myConstant:int = SomeClass.SOME_CONSTANT;
	for(var i:Number=0; i<100000; i++) {
		tmpVar = myConstant;
	}

Variable instantiation

	// This takes 46.52ms to execute
	for(var i:int=0; i<100000; i++) {
		var v1:Number=10;
		var v2:Number=10;
		var v3:Number=10;
		var v4:Number=10;
		var v5:Number=10;
	}

	// This takes 19.74ms to execute
	for(var i:int=0; i<100000; i++) {
		var v1:Number=10, v2:Number=10, v3:Number=10, v4:Number=10, v5:Number=10;
	}
Print Friendly

Tags: ,

Leave a comment

Please upgrade your Flash Player To submit a comment, you must have Flash Player 9.0.0 or higher installed. I use a flash form here to help prevent spam.

3 Comments to Notes: General AS3 Tips

P48l0
January 28, 2010

In Class Reflection you can also cast the object as an Object and call the contstructor property.

Object( some ).constructor //will return the CLASS constructor

gabriel
January 28, 2010

I put together a quick test and that does work. But depending on if performance is an issue the reflect property is still quicker. I’ll update the post with your comment though.

Hosting Best
May 25, 2011

I agree with the objects point. Just write a class, you can do so much more and expand on it if you need to in the future

Search

Ad