Archive for the ‘AS3’ Category

AS3 Array Shuffle

Friday, April 17th, 2009

There are a lot of ways to shuffle an array in AS3, ranging from swapping repeatedly the elements to using the sort function with a random sorter, but the most convenient way I’ve found was to simply splice a random element from the former array to the new shuffled array while the former array has elements.

DaveOnCode has a nice implementation of this method, pasted here:

var arr2:Array = [];

while (arr.length > 0) {
    arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}

Edit: Although small and convenient, this method is not the fastest way of shuffling an array, check out the comments for more info.

Find the value between two colors

Wednesday, March 18th, 2009


A handy function I wrote that calculates a color value between two colors.

function getColor(fromColor:uint, toColor:uint, scale:Number):uint {
    var red:uint = ((fromColor >> 16) + (toColor >> 16) - (fromColor >> 16) * scale) << 16;
    var green:uint = ((fromColor >> 8 & 255) + ((toColor >> 8 & 255) - (fromColor >> 8 & 255)) * scale) << 8;
    var blue:uint = (fromColor & 255) + ((toColor & 255) - (fromColor & 255)) * scale;
 
    return red + green + blue;
}

SWF metadata in pure AS3 projects

Wednesday, February 11th, 2009

When creating pure AS3 projects a metadata is required to define how the swf functions. This metadata is inserted after your import statements and before the main class definiton.

Here are the parameters you can use with the swf metadata tag:

[SWF(width=”x”,
height=”x”,
widthPercent=”x”,
heightPercent=”x”,
scriptRecursionLimit=”x”,
scriptTimeLimit=”x”,
frameRate=”x”,
backgroundColor=”x”,
pageTitle=”x”)]

Example usage:

package  {
	import flash.display.Sprite;
	[SWF(backgroundColor="#FFFFFF", frameRate="30",
		width="550", height="400")]
	public class Main extends Sprite {

	}
}

Swf Class Explorer for AS3

Friday, July 18th, 2008

The ApplicationDomain class in Actionscript 3 is a really handy class. It allows you to get any class from a loaded (or the current) swf and instantiate it.

ApplicationDomain currently has two public methods, hasDefinition(name:String):Boolean and getDefinition(name:String):Object. The simplicity of using these classes is a real treat, instantiating a class is as easy as

var myInstance:DisplayObject = new 
(ApplicationDomain.currentDomain. getDefinition("com.flassari.MyClass"));

The biggest pain about the ApplicationDomain class is however its lack of a function for displaying all of the classes in a particular ApplicationDomain.
ApplicationDomain.getAllDefinitions():Array would be great! But unfortunately we must find our own way of getting around this flaw… so here it is!

I give you, the SwfClassExplorer class. Its usage is simple, as it has only one static function, getClasses(bytes:ByteArray):Array. To use it you must provide it with the bytes of the swf clip you want to explore. Here is an example of basic usage:

public function init():void {
	// Load the swf
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
	urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
	urlLoader.load(new URLRequest("pathToSwf"));
}

private function onSwfLoaded(e:Event):void {
	// Get the bytes from the swf
	var bytes:ByteArray = ByteArray(URLLoader(e.currentTarget).data);
	// And feed it to the SwfClassExplorer
	var traits:Array = SwfClassExplorer.getClasses(bytes);
	for each (var trait:Traits in traits) {
		trace(trait.toString()); // The full class name
	}
}

I’ve prepared an example to demonstrate how it works.
First we have the Primitives.swf file. It will act as our “MovieClip library”. Its library consists of four movieclips, Box, Circle, Polystar and Triangle, each with its linkage class set to its library name:

library

Next we have the Example.swf file. It loads any swf file (Primitives.swf is in the path box by default) and lists all of the classes found in it. If the user clicks any of the classes, it will try to instantiate the class using no parameters (exceptions will be caught and displayed to the user) and if is or extends DisplayObject it will display it in the preview area.

Get Adobe Flash player

You can get the example files and source code here or the compiled swc here.

*UPDATE!
The code is now on GitHub: https://github.com/Flassari/Swf-Class-Explorer
And apparently the swc file has gone MIA, I’ll upload it again if I have the time/can be bothered.