ActionScript Error #1006: A user-defined namespace attribute can only be used at the top level of a class definition.
Description:
This ActionScript error will pop-up when a namespace is declared anywhere besides the class definition. What is a class definition you ask? It is simply the code that names the class – something like ‘public class MyClass’
After the class is defined you are allowed to create custom(or ‘user-defined’) namespace on that level. Anywhere else in the code is not legal. This ActionScript Error is closely related to the Flash/Flex error #1114.
This AS3 error may also just be a typo. you may have typed something like ‘publi class’ rather than ‘public class’ or ‘prvate function’ rather than ‘private function’
Fix:
Either fix the misspelling in your code or place the ‘user-defined namespace’ inside the class definition. The second code example shows a namespace to separate cars and animals. Notice how the bad example nested the ‘user-defined namespace’ inside the constructor function.
Bad Code 1:
package com.cjm.somepackage
{
publi class MyClass {
publi function MyClass() {}
}
}
Good Code 1:
package com.cjm.somepackage
{
public class MyClass {
public function MyClass() {}
}
}
Bad Code 2:
package
{
public class MyClass
{
public namespace Cars;
public namespace Animals;
public function MyClass()
{
Animals var Jaguar:String = “A large cat chiefly of South America”;
Cars var jaguar:String = “A large car chiefly driven in North America”;
}
Cars function run():void {
trace(“At $30/mile”);
}
Animals function run():void {
trace(“On four legs”);
}
}
}
Good Code 2:
package
{
public class MyClass
{
public namespace Cars;
public namespace Animals;
Animals var Jaguar:String = “A large cat chiefly of South America”;
Cars var jaguar:String = “A large car chiefly driven in North America”;
public function MyClass()
{
}
Cars function run():void {
trace(“For 100,00 miles”);
}
Animals function run():void {
trace(“On four legs”);
}
}
}
Adobe has a great explanation of how to use namespaces as well as Collin Moock in Essential ActionScript 3 in Chapter 17
Related AS3 Error:
Flex / Flash Error #1006 addEventlistener is not a function
As always Happy Flashing