Flash CS3 / Flex 2 Error #5001

ActionScript Error #5001: The name of package ‘com.cjm’ does not reflect the location of this file. Please change the package definition’s name inside this file, or move the file

Description:
This ActionScript error is a very common one especially if you haven’t defined a good package and class structure. The simple answer is that you misspelled something in your package description or you saved the class in the wrong folder. (See example 1)

Another reason that you can get this error in Flash is because you have a Document Class and you have specified the classpath in the “Document class:” field and you have something different typed in your package. You might say well that is not possible. If your default classpath is defined in the AS3 setting then the file will compile and the class will work properly if you remove the classpath all together out of your package. This is not recommended because it is not good practice. Please review the example2 below.

Solution:
Check your paths in the package, class and ‘Document Class’.
Example 1

Bad Code

package com.ckm
{

}

Good Code

package com.cjm
{

}

Example 2

Good Code – Bad “Document class:” definition

package com.cjm
{
public class MyDocClass
{
public function MyDocClass()
{
}}}

Flash Document Class Path Bad Example

Good Code – Good “Document class:” definition

package com.cjm
{
public class MyDocClass
{
public function MyDocClass()
{
}}}

Flash Document Class Path

And those are two ways to resolve ActionScript Error #5001

As Always – Happy Flashing

Flash CS3 / Flex 2 Error #1017

ActionScript 3 Error #1017: The definition of base class ImportedClass was not found.

Description 1
You may get this error from importing a custom class that does not have the proper package path into a parent class. Another reason you will get this error is if you forget to import the Class that the subclass is extending(see example 2).

Bad Code 1
(subClass called ImportedClass)

package
{import flash.display.MovieClip;
public class ImportedClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}

Parent Class called ImportingClass

package com.cjm
{
import flash.display.MovieClip;
import ImportedClass;
public class ImportingClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}

Good Code 1

(SubClass called ImportedClass)

package com.cjm
{
import flash.display.MovieClip;
public class ImportedClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}
ImportingClasspackage com.cjm
{
import flash.display.MovieClip;
import ImportedClass;
public class ImportingClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}

Bad Code 2

package com.cjm
{
//import flash.display.MovieClip;
public class ImportedClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}

Good Code 2

package com.cjm
{
import flash.display.MovieClip;
public class ImportedClass extends MovieClip
{
public function ImportingClass()
{
trace(“Message”);}}}

Description 2
ActionScript Error #1017: The definition of base class MovieClip was not found.
This ActionScript Error is due to forgetting to import the MovieClip (or any other  referenced class).

Bad Code 3

package com.cjm.sound
{
public class SoundControl extends MovieClip

Good Code 3

package com.cjm.sound
{
import flash.display.MovieClip;
public class SoundControl extends MovieClip

As always – Happy Flashing

Flash CS3 / Flex 2 Error #1502:

Error #1502: A script has executed for longer than the default timeout period of 15 seconds.

Problem:
You have entered the realm of “Flash Infinite Loop” or at least a really long loop. This is very common when running loops, especially do…while loops.  The timeout is now set to 15 seconds (see image below).

ActionScript Error 1502

Fix:
Make sure that you are not executing a loop that will never break out. Those poor loops running in circles for all of us Flashers/Flexers.

*******************Warning*******************
The Bad Code below will send your Flex/Flash into and infinite loop and will potentially crash your Development Tool. DO NOT COPY and PASTE the BAD CODE EXAMPLES BELOW.

Bad Code:
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i–);
trace(i);
}

Good Code:
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i++);
trace(i);
}

The code examples above have the var i starting with a number that is smaller than the number it is trying to iterate to. This code basically says as long as i is smaller than myLength then loop and each time I loop make i even smaller.

Bad Code:
*********This code crashed my Flash multiple times.*******

var myArray:Array = [“joe”, “bob”,”pam”];
var randNum:int = Math.round(Math.random()*myArray.length-1);
var curNum:int = 1;
do {
trace(randNum);
} while (randNum == curNum);

The key to using do…while loops in Flash is to make sure that the variable that you are validating against is changed inside the loop itself. Unless the variable that you are validating against changes within the loop itself it will loop forever. This example of a do…while loop is valid but will very likely put Flash into an infinite loop because the likelihood that the number will be 1 is very high, especially considering that the code is rounding and the number 1 is the middle digit of the three. This loop can execute quickly if Flash happens to choose another number or it can loop infinitely. The reason you might be doing this kind of loop is because you want to pull things out of an array, xml, or some other list and don’t want to get duplicates. A better way to do this is to either use the splice() methods in Flash or Flex or if you don’t want to affect the original array you can slice() out a duplicate without the curNum value and then the value will never be repeated because it no longer exists in the items that you are evaluating against. That way you know for sure the item that you don’t want repeated is gone.

There are many other ways to run into an infinite loop. These are just a couple. Feel free to post any questions you have in the comments section and I will answer them promptly.

Thanks and Happy Flashing.

Flash CS3 / Flex 2 / AS3 Error #1176

Flash CS3 / Flex 2 / AS3 Error #1176: Comparison between a value with static type String and a possibly unrelated type int.

Problem:
Flash won’t compile because it doesn’t like to compare an int with a String.

Fix:
Thank heavens for Strict Typing. Without it we wouldn’t get such nice errors. Just change the type from an “int” to a String and you will be on your way.  If you need it to really be an int then you can cast the second variable to be an int rather than a String. See code example below

Bad Code:

var dropNum:int;function bob():void
{
var myText:String = “2”;
var curNum:String = “3”;
dropNum = myText.substr(0,1);
if (curNum == dropNum) {
someCode();
}
}

Good Code:

var dropNum:String;

function bob():void
{
var myText:String = “2”;
var curNum:String = “3”;
dropNum = myText.substr(0,1);
if (curNum == dropNum) {
someCode();
}
}

or

var dropNum:int;

function bob():void
{
var myText:String = “2”;
var curNum:int = 3;

dropNum = Number(myText.substr(0,1));
if (curNum == dropNum) {
someCode();
}

Hope this helps solve ActionScript Error 1176 and Happy Flashing

Curtis J. Morley

Flash CS3 / Flex 2 AS3 Warning: #3596: Duplicate variable definition.

Warning: 3596: Duplicate variable definition.

Problem:
You are using the same variable and Flex/Flash doesn’t like it.
Fix:
Change the name of one instance or unnest your loop.

Bad Code:

var myArray1:Array = [“a”,”b”,”c”,”d”,”e”];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var i:int=0; i < myArray1.length; i++)
{
trace(myArray2[i][i]);
}
}

Good Code:

var myArray1:Array = [“a”,”b”,”c”,”d”,”e”];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var j:int=0; j < myArray1.length; j++)
{
trace(myArray2[i][j]);
}
}

or

var keys:String = “Word”;
for (var i:int=0; i < keys.length; i++)
{
//Code
}

for (i=0; i < keys.length; i++){
//More Code
}

Once Again – Happy Flashing

Flash CS3 / Flex 2 AS3 Error #1046

Error #1046: Type was not found or was not a compile-time constant: Name.

Description:
One reason you will get this error is if you have an object on the stage with the same name as an object in your library. So if you are trying to dynamically add a movieClip from your library that has a Class name (see image below) that is the same as the name of an instance on the stage it will give you this error. What happens when you add a class name in the library is that Flash (in this case) will create a class around that object. When you have an instance on the stage that has this same name Flash doesn’t know how to handle it because the Class was not properly instantiated.

AS3 Error 1046 Fix 1:
Remove the instance from the stage, change the name of the instance, or remove the Class linkage in your library.

Flash Error 1046: Type was not found or was not a compile-time constant:

AS3 Error 1046 Fix 2:

Import, Import, Import. Another common reason that you will get this error is because you have not imported everything you are including in your movieClip. For example if you are using textFields inside your movieClip you need to include the following line of code:

Good Code:

import flash.text.TextField;

By importing the TextField object the compiler now knows what to do with that textField inside your movieClip. This will eliminate another version of this error – ‘1046: Type was not found or was not a compile-time constant: TextField’.

Update:

AS3 Error 1046 Fix 3:

This error will also show up if you are trying to instantiate a library object with an instance name that is the same as the AS Linkage in your library. In other words, you can’t call it the same name. Look at the image for the Linkage name and then take a look at the code examples. The red code below shows that the same name is being used.

AS3 1046 Error - Library AS Linkage Name

Bad Code:

var TTA:TTA = new TTA();

Good Code:

var thirdAlternative:TTA = new TTA();

 

And now you know three ways to resolve ActionScript Error #1046: Type was not found or was not a compile-time constant:
As always Happy Flashing!

Flash CS3 / Flex 2 AS3 Error #1078

Error #1078: Label must be a simple identifier.

Description:
The main reason you will get this ActionScript error comes because of mistyping a colon. You can get AS3 Error 1078 by putting a a colon at the end of a line instead of a semicolon. You can get it by trying to ‘Type’ an object without the ‘var’ keyword, and my personal favorite – If you are in Flash writing a Class and hit the quick keys esc >> t >> r to get a trace(); statement you will get trace():void; instead.

Fix 1:
This error can be resolved by correctly typing a semicolon.

Bad Code1

myText.border = false:

Good Code1 :

myText.border = false;

Fix 2:
This can be resolved by using the keyword var. (The bad code example here is really bad code)

Bad Code 2:

this.box:MovieClip = new MovieClip();

Good Code 2:

var box:MovieClip = new MovieClip();

Fix 3:
Delete the silly extra stuff that Flash puts in there for you. If you have the bad code as shown below you will most likely get another error like 1083. Just fix the first and the second will also be resolved.

Bad Code 3:

trace():void

Good Code 3:

trace();

And that is how you solve ActionScript Error #1078

Happy Flashing

Flash CS3 / Flex 2 AS3 Error#1168

Error #1168: Illegal assignment to function setTextFormat.

This is another easy one. It just means that you assign the format within parenthesis rather than after the “=” sign.
Bad Code:

var myText:TextField = new TextField();
var myFrmt:TextFormat = new TextFormat();
myFrmt.color = 0x336699;
myFrmt.font = “Franklin Gothic Book”;
myText.text = “Flash is fun – Happy Flashing Everyone”;
myText.border =true;
myText.wordWrap = true;
myText.autoSize = TextFieldAutoSize.CENTER;
myText.setTextFormat = myFrmt;
this.addChild(myText);

Good Code:

var myText:TextField = new TextField();
var myFrmt:TextFormat = new TextFormat();
myFrmt.color = 0x336699;
myFrmt.font = “Franklin Gothic Book”;
myText.text = “Flash is fun – Happy Flashing Everyone”;
myText.border =true;
myText.wordWrap = true;
myText.autoSize = TextFieldAutoSize.CENTER;
myText.setTextFormat(myFrmt);
this.addChild(myText);

And this is how you resolve Error #1168: Illegal assignment to function setTextFormat.

Flash CS3 / Flex Error Messages #1010:

So that you don’t have to figure out all the cryptic error messages that are being thrown from Flash 9 and Flex I have decided to compile a list of error messages and what they mean in a practical sense.

TypeError: Error #1010: A term is undefined and has no properties.

What this error means is that you are trying to access a property of an object that isn’t there. This is very common when using Arrays. This error will also pop-up if you are trying to access a property of an Object that has not been assigned/created.
Bad Example 1:

for (var i:uint=0; i<=internalArray.length; i++) {
if (internalArray[i].length > 10){
trace(internalArray[i].length);
}
}

Good Example 1:

The above example will throw the error because you are trying to access 1 extra element on the end of your array. Just remove the final loop by removing the “=” sign in the comparison. Because Arrays are “0 based” i<=internalArray.length will take you beyond the last index of the array and try and evaluate for a value that is not there. For example var newArray:Array = [“Bob”, “Joe”, “Jim”]; has a length of 3 but Jim has an index of 2.

for (var i:uint=0; i<internalArray.length; i++) {
if (internalArray[i].length > 10){
trace(internalArray[i].length);
}
}

Bad Example 2:

for (var i:uint=0; i 10){
trace(internalArray[i].bob);
}
}

Good Example 2:

The above example will not work because the property bob has not been defined anywhere in the code. You can remedy this by removing the reference to “bob” or by declaring what the “bob” property for each item in the array.

for (var i:uint=0; i 10){
trace(internalArray[i]);
}
}

Bad Example 3:

This example tries to access a property that is created(instantiated) a few lines down.

for (var i:uint=0; i 10){
trace(internalArray[i]);
}
}

var internalArray:Array = [“bob”,”jim”,”joe”];

Good Example 3:

The order of operation is important because even though Flash/Flex does some “hoisting” you will still get errors if you don’t order things properly.

var internalArray:Array = [“bob”,”jim”,”joe”];

for (var i:uint=0; i
if (internalArray[i].length > 10){
trace(internalArray[i]);
}
}

As always – Happy Flashing