1 Reply Latest reply on Nov 8, 2013 7:08 PM by Ned Murphy

    Communication between the Document Class and MovieClip Class: error 1009

    joeljiverson

      am learning how to make MovieClip classes. All I am trying to do is  give the MovieClip the ability to move with the left and right arrow  keys, but it's not working.

      Here is my code for my Document Class Main:

       

      package  {   
      
          import flash.display.MovieClip;
      
          public class Main extends MovieClip{
      
               var blueBall:Ball = new Ball; //This is line 7
      
                public function Main()
              {
                  addChild(blueBall)
              }
      
          }
      
       }

       

      My code for my Ball class is:

       

      package
      {
          import flash.display.MovieClip;
          import flash.events.KeyboardEvent;
          import flash.ui.Keyboard;
      
          public class Ball extends MovieClip
          {
              public function Ball()
              {
                  stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); //This is Line 11
              }
      
               function keyPressed(evt:KeyboardEvent):void
              {
                   switch (evt.keyCode)
              {
                  case Keyboard.RIGHT :
                      this.x +=  10;
                      break;
                  case Keyboard.LEFT :
                      this.x -=  10;
                      break;
              }
          }
      
      }

       

      }

       

      When I run this in the debug, I get this error:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.     at Ball()[C:\Users\Joel\Desktop\Flash\Projects\DropClassTest\Ball.as:11]     at Main()[C:\Users\Joel\Desktop\Flash\Projects\DropClassTest\Main.as:7]

       

      Now, if I just leave my Main document class blank, and just drag and  drop the blueBall Movieclip out of the Library, it works the way I want  it to, but once I try to use the document class to do it, it's not  working.

       

      Needless to say, I am quite lost, even thought the debugger is telling me what lines are wrong.

       

      Any ideas?