Flash Player 10 penetration at 75.3%

April 27th, 2009

Adobe has just updated their flash penetration statistics, and flash player 10 has reached 75.3% in Europe.

Soon it will be time to make 10 the default version in my publish settings =)

AS3 Array Shuffle

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

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

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 {

	}
}

Line-Line Intersection in C++

November 3rd, 2008

Line line intersection function explanation

I’ve just been tasked with creating a function to get the intersection of two lines.
With the help of equations from Wolfram Mathworld I created this nifty function:

Point* intersection(Point p1, Point p2, Point p3, Point p4) {
	// Store the values for fast access and easy
	// equations-to-code conversion
	float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
	float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

	float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
	// If d is zero, there is no intersection
	if (d == 0) return NULL;

	// Get the x and y
	float pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
	float x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
	float y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;

	// Check if the x and y coordinates are within both lines
	if ( x < min(x1, x2) || x > max(x1, x2) ||
		x < min(x3, x4) || x > max(x3, x4) ) return NULL;
	if ( y < min(y1, y2) || y > max(y1, y2) ||
		y < min(y3, y4) || y > max(y3, y4) ) return NULL;

	// Return the point of intersection
	Point* ret = new Point();
	ret->x = x;
	ret->y = y;
	return ret;
}

Hope it will be to as good use to you as it is to me.

Swf Class Explorer for AS3

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.

Iceland Socks Outtakes

July 9th, 2008

I wanted to share with you some takes that didn’t make it into the final version of Iceland Socks. When you mix the good people of Gogogic with beverages and sock puppets you’ll end up with more outtakes than usable footage.
Enjoy