25
Aug/08
0

Queue loader items in an AS3 class

I wrote a class lately to queue loader items in AS3. I use it to load textures for my 3D engine but you can use it without modification for the loading of other files. The class is very simple, and doesn’t give any information about the loading progress apart from when it finishes loading all files. The class comes in handy when trying to load multiple files and have one event listener when all files are loaded. Below an example of the implementation:

[sourcecode language='actionscript']import qLoader; // You can change the package to for example com.input.qLoader;
var loader:qLoader = new qLoader();
// You can add relative file locations, but SWF’s online will likely
// only accept url’s on the same domain.
// You can activate debug traces by enabling the debug mode once the
// class has been created by uncommenting the following line:
// loader.mDebug = true;
loader.addLoad(”textures/stone.jpg”);
loader.addLoad(”textures/stone2.jpg”);
loader.addLoad(”textures/stone3.jpg”);
loader.addLoad(”textures/stone4.jpg”);
loader.addLoad(”textures/stone5.jpg”);
loader.addEventListener(”completeQ”,onLoaded);
// Initiate the loader
loader.initiate();

function onLoaded(evt:Event):void {
// In this example I create a bitmapData object
var bm:BitmapData = new BitmapData(loader.mFinishedItems["textures/stone.jpg"].width,loader.mFinishedItems["textures/stone.jpg"].height,false);
bm.draw(loader.mFinishedItems["textures/stone.jpg"],new Matrix());
}[/sourcecode]

The class needs to be an extension on the EventDispatcher class to work (otherwise it cannot dispatch events). I might update the class with progress info (like 100/4555Kb) or something and the loadNext() and loadItems() functions are the same but I’m too lazy for that now.

qLoader.as

Tagged as: , ,
20
Aug/08
3

Bitmap skewing class

Finally, after two days of trial and error, I got my bitmap skewing class working. The class is based upon Flash and Math’s work, especially the algorithm for ‘triangulating’ (I know this is not the right word) distorted quadrilaterals. The class is quite complete there might be another function for simply rendering textures onto triangles instead of quadrilaterals. See the class in action below, adjust the “AA” level (I know it’s not really the same, but couldn’t think of a nicer term) to increase or decrease the level of precision. The higher the AA level the better it looks. Lower AA levels increase performance. The same counts for the noise reduction, which decreases the performance when turned on.

Download the skew.as class

The usage is fairly simple;

[sourcecode language='actionscript']import com.skew;
// First boolean is for noise reduction the second for debug mode
var sk:skew = new skew(false,false);
// For skews with a greater precision than 1
sk.AASkew(a,b,c,d,bitmap,sp,AAh,AAv);
// For skews with a precision of 1
sk.transformer(a,b,c,d,bitmap,sp,AAh,AAv);
// With a,b,c,d being arrays containing 2 dimensional points e.g. a = [10,10];
// And bitmap being a bitmapData object
// sp being a sprite or shape
// AAh and AAv respectively being the horizontal and vertical “AA” levels[/sourcecode]

The sk.transformer function is quite an increase in performance from the sk.AASkew function because it doesn’t require mapping the non distorted bitmap.

EDIT: Updated the skew class with a better variable name for the smoothing parameter and corrected the skew functions to smooth both halves. Thanks to Banana.