Tag Archives: AS3

ActionScript 3.0

SmoothBitmap – How to ensure Bitmap pixel smoothing in AS3

A common oversight in Flash projects, when using a Bitmap with loaded content is that Flash will revert a Bitmap’s smoothing parameter to false when you replace its bitmapData. That is, when the data loads into the Bitmap, anti-aliasing will get turned off. This is simple enough to fix, but since you may not know if someone is going to replace the bitmapData of a Bitmap you have created – then it’s much better to code defensively for it.

This little SmoothBitmap class is for just such an occasion. Instantiate it like a regular Bitmap and, no matter what another developer does with it, smooth, anti aliased pixels when scaling/rotating will be ensured. Enjoy 🙂

CODING WRONGS – Where do I start with the bad?

It gets scary out there sometimes. During my freelance career I’ve worked at a lot of different companies and have seen such coding horrors as you cannot imagine. So I thought I’d start immortalising some of them – so that we can all learn better coding practices, by looking at the bad.

Starter for 10 – What’s wrong with this picture?

public function doSomething(stuff:String):void
{
	var newPixels:BitmapData = new BitmapData(someUint, someUint, true, 0);
	someBlitUtil.blitStuffTo(newPixels, stuff);
	someBitmap.bitmapData = newPixels;
}

Did you spot the fubar? It’s not an obvious one. Continue reading CODING WRONGS – Where do I start with the bad?

Loan Shark – fast object pooling utility

LoanShark AS3 Object Pooling UtilityA couple of years ago, I created an object pooling utility for a games project I was building in AS3. Since then, I’ve used it quite a few times, in order to speed up apps and improve resource management, easing the load on the garbage collector by reusing objects instead of recreating them. Continue reading Loan Shark – fast object pooling utility

Fastest way to add multiple elements to an Array / Vector in AS3

In a simple situation, where you wish to add many elements to an Array or Vector, you might just do:

var input:Array;
var output:Array;
while (input.length)
output.push(input.shift());

However, the sizes of both Arrays are manipulated for each loop, which will have an adverse impact on speed and memory usage. So, we could cache the length of the input Array and not manipulate it:

var input:Array;
var output:Array;
var inputLength:uint = input.length;
for (var i:int = 0; i < inputLength; i++)
output.push(input[i]);

But we’re still growing the size of the output Array incrementally, which is very bad. Since we know input.length in advance, we could grow the output Array to its new size just once, before the loop:

var input:Array;
var inputLength:uint = input.length;
var output:Array;
var outputLength:uint = output.length;
var newOutputLength:uint = outputLength + inputLength;
output.length = newOutputLength;
for (var i:int = 0; i < inputLength; i++)
output[i + outputLength] = input[i]);

This is OK, but still involves a loop. If only we could push multiple elements into the push method in one go. Well, we can – enter the apply method. Since Array.push accepts multiple arguments (something rarely used) and apply allows us to pass an Array of arguments to any Function, one line and we’re done:

var input:Array;
var output:Array;
output.push.apply(null, input);

This works out faster and more memory efficient than the other methods. It works nicely for Vectors, too. If anyone has a faster method of doing this, do let me know.