Typing

From ActionScriptWiki

Jump to: navigation, search

Custom objects should be always typed as such since the Flash Player can perform very important optimizations in such a case. An example could be a simple three dimensional vector. Such an object has usually a x, y and z property.

[edit] Wrong Version

In this case the (x,y,z) information is stored in an Object. This is bad because the Flash Player does not know about the object and it is also very bad practice in terms of refactoring and OOP.

for( var i: int = 0; i < 1024; ++i )
{
	var vector: Object = {};

	vector.x = 1.0;
	vector.y = 2.0;
	vector.z = 3.0;
}

[edit] Correct Version

For the (x,y,z) properties a class is created. The Flash Player knows now that such an object only holds three Number values and can perform specific optimization.

package
{
	public final class Vector3D
	{
		public var x: Number;
		public var y: Number;
		public var z: Number;
	}
}
for( var i: int = 0; i < 1024; ++i )
{
	var vector: Vector3D = new Vector3D();

	vector.x = 1.0;
	vector.y = 2.0;
	vector.z = 3.0;
}
Personal tools