AS3 Library Headaches
I’ve been yanking my hairs out for the past few days regarding a few of my algorithms cracking. It turns out that a there area a few classes in the AS3 library that are a little tricky. Here are my findings so far:
Rectangle class
Negative Dimensions
This class works fine IFF you ensure your rectangles have positive dimensions. If you attempt to query the top,left,right or bottom properties from a rectangle with any dimension as being negative, then the class breaks and the values you get back will not be the values you would expect. In other words, if height is negative then top will actually return the physical bottom of the rectangle. One example where you may have rectangles with negative dimensions is drag regions.
This issue is not that bad if you were expecting it to behave like this, or if you anticipated this scenario and made the adjustments accordingly when say updating a drag region.
Here’s my test:
var rect : Rectangle = new Rectangle(0, 0, -50, -50);
trace("rect = " + rect);
trace("\nrelative properties:");
traceRect(rect);
trace("\nrelative properties corrected (as they should be):");
traceCorrectedRect(rect);
function traceRect (pRect : Rectangle) : void
{
trace("top=" + pRect.top + " left=" + pRect.left + " bottom=" + pRect.bottom + " right=" + pRect.right);
}
function traceCorrectedRect (pRect : Rectangle) : void
{
var t : Number = pRect.top;
var b : Number = pRect.bottom;
var l : Number = pRect.left;
var r : Number = pRect.right;
if (pRect.width < 0)
{
l = pRect.x + pRect.width;
r = pRect.x;
}
if (pRect.height < 0)
{
t = pRect.y + pRect.height;
b = pRect.y;
}
trace("top=" + t + " left=" + l + " bottom=" + b + " right=" + r);
}
Here’s the output:
rect = (x=0, y=0, w=-50, h=-50) // relative properties: top=0 left=0 bottom=-50 right=-50 // relative properties corrected (as they should be): top=-50 left=-50 bottom=0 right=0
Setting Top Left
Another tricky area I ran into with the Rectangle class is when you go to set the top left corner of the rectangle. What actually happens here (and even when you set top and left individually) is the top and left get updated properly as expected but, the width and height are modified as well. What you would expect is that the top and left would just position your rectangle and not explode it. This isn’t so bad either so long as you use x and y to position your rectangle.
Here’s my test:
var rect : Rectangle = new Rectangle(50, 50, 50, 50);
var copy : Rectangle;
trace("rect = " + rect);
trace("\nsetting top and left to 0");
copy = rect.clone();
copy.top = 0;
copy.left = 0;
trace("rect = " + copy);
trace("\nsetting topLeft to (0,0)");
copy = rect.clone();
copy.topLeft = new Point(0, 0);
trace("rect = " + copy);
trace("\nreseting the rect");
copy = rect.clone();
trace("rect = " + copy);
trace("\nsetting the x and y to 0");
copy.x = 0;
copy.y = 0;
trace("rect = " + copy);
Here’s the output:
rect = (x=50, y=50, w=50, h=50) // setting top and left to 0 rect = (x=0, y=0, w=100, h=100) setting topLeft to (0,0) rect = (x=0, y=0, w=100, h=100) // reseting the rect rect = (x=50, y=50, w=50, h=50) // setting the x and y to 0 rect = (x=0, y=0, w=50, h=50)
Array class
Array As Argument: splice
The splice operation is described in the docs as treating each element in an Array argument separately, and not just inserting the Array itself. This is not that case as it turns out.
Here’s my test:
trace("Test several methods of the Array class with an Array as a parameter.\n");
var array : Array = new Array(1,2,3);
var newArray : Array = new Array(4,5,6);
var copy : Array;
trace("The following Arrays are used in the test:");
trace("array = " + array);
trace("newArray = " + newArray);
trace("\nIn each case the operation specified is performed on 'array' and 'newArray'\n"+
"is passed as an argument to the operation.\n");
trace("\nconcat:");
traceArray(array.concat(newArray));
trace("\nsplice:");
copy = copyArray(array);
copy.splice(3, 0, newArray)
traceArray(copy);
trace("\nThese results show that the only operation that behaves like it is documented is the concat() operation.");
function copyArray (pArray : Array) : Array
{
return (pArray.slice(0));
}
function traceArray (pArray : Array) : void
{
var len : int = int(pArray.length);
for (var w : int = 0; w < len; w++)
{
trace("element [" + w + "] = " + pArray[w]);
}
}
Here’s the output:
Test several methods of the Array class with an Array as a parameter.
The following Arrays are used in the test:
array = 1,2,3
newArray = 4,5,6In each case the operation specified is performed on ‘array’ and ‘newArray’
is passed as an argument to the operation.concat:
element [0] = 1
element [1] = 2
element [2] = 3
element [3] = 4
element [4] = 5
element [5] = 6splice:
element [0] = 1
element [1] = 2
element [2] = 3
element [3] = 4,5,6These results show that the only operation that behaves like it is documented is the
concatoperation.



