Flex / Flash Error 1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.

ActionScript 3 Error: 1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.ActionScript 3 Error #1042

AS3 Error 1042 Description:

AS3 Error1042 is actually a really helpful error.  It is simple in that it states that you will not be able to access something called “this” at the point in the code you are trying to access it.  In other words, you can’t put “this” directly inside the package or the class.  It can go in any of the functions that are within the Class but no higher.

It makes sense.  If you think about trying to access “this” from inside the package or from inside the class definition then you should understand why.  Packages are used only to show Flash/Flex where to find the code so there is no “this” in a file structure.  Also at the Class definition level this doesn’t exist either because the Class hasn’t yet run the constructor to make a “this”.

Flex / Flash Error #1042 Fix:

Remove the reference to the “this” keyword in your package or Class.

Bad Code:

package com.cjm.teaching{
import flash.display.MovieClip;
trace(this);
public class Ball extends MovieClip {
public function Ball():void {
}
}
}

or

package com.cjm.errors{
import flash.display.MovieClip;
public class MyClass extends MovieClip {
trace(this);
public function MyClass():void {
}
}
}

Good Code:

package com.cjm.errors{
import flash.display.MovieClip;
public class MyClass extends MovieClip {
public function MyClass():void {
trace(this);
}
}
}

Related Errors:

If the reference to “this” is at the package level you will also get AS3 Error 5000

This should help you resolve Flex / Flash Error #1042

Thanks and as always Happy Flashing

Curtis J. Morley