Despite their seemingly over-abundance while getting started with AS3, the new error messaging is extremely helpful and specific. While error messages are tedious at first, they eventually become extremely helpful once you understand what they mean. If you make a mistake, they tell you exactly what you did wrong.
So, here’s a short list of errors that I commonly encounter due to routine code clumsiness.
A. Compiler Errors (will prevent a movie from publishing.)
1067: Implicit coercion of type [datatype] to an unrelated type [datatype].
While the wording is fancy, the nature of this error is extremely simple: you’re trying to put a square peg into a round hole; or in technical terms, your data types don’t mesh. An example scenario that would cause this error is this:
var testing:int = new Date();
Notice the problem? The “testing” variable is typed as an “int” but is getting a “Date” object assigned to it. AS3 is very strict about data types, so will force you to make sure they always line up.
1120: Access of undefined property [name].
You’re trying to reference a value that was never created. If you get this while referencing a property of another object, you’re probably trying to access a property that is not defined as a public member of that object’s class. If you get this error in response to a value that you’ve defined within the script that you’re writing, then you probably forgot to declare the value with a “var” statement before its first reference.
1151: A conflict exists with definition [name] in namespace internal.
You’re trying to declare a value that has already been created. Make sure that you haven’t declared multiple variables by the same name within the same script. A common “gotcha” that I get hit with comes from using the same iterator for multiple loops, like so:
function errorDemo():void
{
var list:Array = new Array(0, 1, 2);
for (var i:int = 0; i < something; i++) {
// do something.
}
for (var i:int = 0; i < something; i++) {
// do something else.
}
}
Notice the error? I’ve declared the “i” iterator variable with a “var” statement twice. To fix that, I’d need to use a different iterator name for the second loop, or else just declare the second iterator as “i = 0″.
B. Runtime Errors (will NOT prevent movie publishing; occur at runtime.)
Error #2006: The supplied index is out of bounds.
You’re trying to reference a depth (index) within an object’s display list that does not exist. For example, you’ll encounter this error if you call “removeChild(2);” on a display list that only has one child object. The third child that you referenced there (yes, third) does not exist. Keep in mind that display lists are zero-indexed, so the lowest item in the list has an index of “0″ and the highest item in the display list has an index of “numChildren-1″.
Error #2007: Parameter child must be non-null.
You’re trying to supply a child-accessor method (addChild(), removeChild(), getChildAt(), etc) with a null object reference. An example scenario that would cause this error is this:
var child; removeChild(child);
While the above example will compile, it will fail at runtime because the child reference is not populated with a valid DisplayObject target before being used in a child-accessor method.
Error #2025: The supplied DisplayObject must be a child of the caller.
Tricky one. The foundation principal behind this error is the definition of a parent-child relationship: a parent object contains a child object within its display list RIGHT NOW. As soon a child is removed from its parent’s display list, their parent-child relationship is broken. So, this error occurs when you perform an action like using “removeChild()” on an object that is not contained within the target’s display list. In other words, an object cannot remove a child that it does not contain. There are several patterns that you can use to avoid conflicts. Among my most common is this:
var child:DisplayObject;
if (child != null && this.contains(child))
{
removeChild(child);
}
The above pattern will avoid both a #2007 and #2025 error, since we’ve validated that the child both exists and that it is contained within our display list before trying to remove it. Just be sure to test that the object is not null FIRST, otherwise the “contains()” operation could throw a #2007 error.
Another common cause of the #2025 error is when a child-accessor method tries to work with children that are contained within another object’s display list. It doesn’t work. Child-accessor methods only work on the callee’s own children. So, when working between object scopes, I find the most reliable method to work with child displays is to call all operations relative to the child, like so:
child.parent.removeChild(child);
In the above example, we can call this operation from any scope that has a reference to the child object, and we’re sure that the operation will target the correct parent.
Posted by bigmac
Posted by bigmac
Posted by bigmac