Universal Application To Module Api

While working on my latest project, I was supposed to came up with some elegant solution (api) to make 3rd party modules be able to communicate with the main application and vice versa. In fact I already did the approach some time ago, now it was time to improve it and implement into a flex framework.

The api is based on event dispatching. Instead of sharing interface and calling some public available functions / callbacks (requires functions defined in compile time), event based api gives you the freedom of registering and releasing listeners anytime in runtime…

Read the rest of this entry »

The Ultimate Resource Manager

How would you like your resousrce manager to be able to access bundle content as easy as RM.bundleName.resourceName no functions used, no parameters, even better with a binding enabled? You like it? Me too. Lets have a look at the implementation.

First we need dynamic variables to be bindable. There are more existing and working solutions for this based on Proxy class. We have to handle getProperty() method and implement IEventDispatcherin order to dispatch events. The final class may looks something like BindableDynamics.

Next, lets do some ResourceManager magic to obtain localized values for our dynamic properties. We do not expect the properties will dispatch changes within one locale, but when locale changes (e.g. switch from en_US to fr_FR) all properties should get updated. A single artificial “propertyChange” event will do a good job for us. Lets call this DynamicResourceManager.

Final step can be custom. For example I like implementation with static (no initialization required) constants (constant works with mxml binding without throwing a warning) that looks something like…

Read the rest of this entry »

Faster floor, round, ceil for ActionScript

It is always nice to add some speed boost to your performance critical algorithms. Making some native Math function’s alternatives perform up to 8 times faster is possible. Lets have a look at the basic ones Math.floor(), Math.round(), Math.ceil():

faster alternative for Math.floor()

var ni:int = n;
return (n < 0 && n != ni) ? ni - 1 : ni;

faster alternative for Math.round()

return n < 0 ? n + .5 == (n | 0) ? n : n - .5 : n + .5;

faster alternative for Math.ceil()

var ni:int = n;
return (n >= 0 && n != ni) ? ni + 1 : ni;

Read the rest of this entry »

Using Metadata Frame for a Preloader

This article was sleeping in my drafts for a while, and I am happy I managed to finish it today. There are multiple ways to create preloader for your ActionScript applications with flex compiler. The easiest and my prefered way is to use Frame metadata.

This metadata is a hint to the compiler that the class containing the metadata would like to be bootstrapped by the cited factory. In other words, when you build an application based on mx.core.Application, that isn’t actually the “root” class of the SWF. The root class is actually a compiler-generated subclass of mx.managers.SystemManager (Modular Applications)

Read the rest of this entry »

Quick Tip: Multi-Dimensional Vectors

Have you ever thought of creating or using multi-dimensional Vector in ActionScript? Yes, it is possible and the definition syntax comes pretty logical to me now ;-).

var list:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();

…or shorter declaration:

var list:Vector.<Vector.<int>> = new <Vector.<int>>[];

now your vectors have two dimensions, I mean:


UNIPacket – Standardize NetGroup, NetStream Raw Messages

UNIPacket and UNIPacketHeader classes is my attempt to standardize communication within NetStream and NetGroup communication. While both Net* classes enables you to send any raw data between two instances of flash, you should carefully handle incomming messages. Imagine you are willing to send additional info with your message (sender, id etc.). That is where UNIPacket classes comes into scene, both are very simple and able to selfcreate from incomming object.

UNIPacket contains only two variables: header:UNIPacketHeader and data:* (holds the actual data being sent).

UNIPacketHeader is dynamic class with prepared variables: id:uint (numeric identificator of message), hash:String (string identificator of message), sender:String (use for sender identification).

Read the rest of this entry »

Quick Tip: Working with NetStream

While working on my brand new P2P project I have come to some issues and solutions, that may help you with your projects. First thing, the NetStatusEvent codes. You may expect that codes are dispatched by the instance of the class they beigins with (e.g. “NetStream.Connect.Success” dispatched by NetStream), but in fact, this does not happen (“NetStream.Connect.Success” is dispatched by NetConnection). I bellieve its not a bug but a feature by flash player developer team. To make this work as expected you may do the following:

netConnection.addEventListener(type, onNetStatus);
function onNetConnectionNetStatus(event:NetStatusEvent):void
		case "NetStream.Connect.Success":

Read the rest of this entry »

Quick Tip: uint To 6 Digit RGB Hex

Shortest possible, simple / no looping method of getting hex string from color is:

var color:uint = 0x00ff00;
var hex:String = color.toString(16);

trace("00000".substr(0, 6 - hex.length) + hex);
// output 00ff00

trace("#00000".substr(0, 7 - hex.length) + hex);
// output #00ff00

Quick tip: always use global position for hitTestPoint()

Just for the record and myself (I should read all the documentation, also the long sentences 🙂 … Make sure you always check hitTestPoint() against global coordinates of some point.

// we are somewhere deep in myObject.myObject2.myInnerObject
var point:Point;
var someMovingObject:DisplayObject;
var hitShape:DisplayObject;

point = localToGlobal(new Point(someMovingObject.x, someMovingObject.y)); 
point = someMovingObject.localToGlobal(new Point());
// both give us same results, 
// 2nd method is faster in loops while it can use predefined zero point

hitShape.hitTestPoint(point.x, point.y, true);

Even hitShape object is not directly on a stage (but on myInnerObject), you have to use global points for testing hits.

Quick tip: Timer Listeners Doesn’t Require Remove

Based on profiler (or net.hires.debug.Stats) your Timer listeners doesn’t need to be removed in order to empty garbage collector – the release reference, however it is always a good practice to remove all of them, event those with anonymous functions. Quick proof of concept:

Read the rest of this entry »