views:

1128

answers:

3

I simply create a movie clip, put it on the stage and give it an instance name of char.

Here's my document class, simple as can be:

   public class Test extends MovieClip
        {

         public function Test() 
         {
          char.x = 1315; char.y = 459;
                addEventListener(Event.ENTER_FRAME, mouseListen);

         }

            private function mouseListen(e:Event) {
          char.x = mouseX;
          char.y = mouseY;
         } 

        }

And I get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at com.me::Test/::mouseListen()

How in the world is it possible that it knows what "char" is in the constructor but not in the mouseListen function? What else could it be?

A: 

I rebuilt this locally and it works fine. Follows the mouse. Any other code that's missing? I noticed you didn't have a package definition. Is this class the document root of the main file, or are you loading this swf into another swf?

package
{
    import flash.display.*;
    import flash.events.*;

    public class Test extends MovieClip
    {

        public function Test() 
        {
            char.x = 1315; char.y = 459;
            addEventListener(Event.ENTER_FRAME, mouseListen);
        }

        private function mouseListen(e:Event) 
        {
            char.x = mouseX;
            char.y = mouseY;
        }   
    }
}
Typeoneerror
Yes, I just didn't include the package definition. Obviously it's finding the class since it's throwing the error in mouseListen(). I get the "char" to follow the mouse, but it throws the error first.
Also, if I say char.alpha = 0, it doesn't work (char is still visible).
Did you give char an instance name on the stage? do you have "automatically declare stage instances" turned on in your flash prefs?
Typeoneerror
A: 

Had the same issue and just solve it (I think). I moved the layer my buttons were to the top, above my code layer and voila...a stupid flash thing. I then move it down one layer below and it still worked just not two layers down where I had it.

newenay
A: 

(1) char is a reserved word. Don't use it.

(2) the first step to debugging a 1009 is to do existence checks before trying to change things. It will let you determine if its something silly like the object isn't on the stage yet because its added in a later keyframe, etc.

In this case you don't need to do that, though, because your scope is off. In the event listener, since you added it to char, inside of the function itself you will get at 'char' with the 'this' keyword.

Try the following:

public function Test() 
    {
        char.x = 1315; char.y = 459;
        addEventListener(Event.ENTER_FRAME, mouseListen);
    }

    private function mouseListen(e:Event) 
    {
        this.x = mouseX;
        this.y = mouseY;
    } 
Jason M