Skip to main content
Participant
March 9, 2010
Question

classes

  • March 9, 2010
  • 1 reply
  • 531 views

hey everyone, so far actionscript hasnt been my biggest friend every line i type returns me with a error but that's likely me ^.^.
anyway i'm trying to load a class into my main.AS which is linked to my flash background. however errors errors and more errors floating my screen.
<code>

package
{
import flash.net.URLRequest;
import flash.display.*;
import flash.events.*;
 
public class Main extends MovieClip
{
  var playerShip:Ship = new Ship();
  //var enemyShip = new mc_EnemyShip();

 
  function Main()
  {
   addChild(playerShip);
   //trace(Ship);
  }
}
}

</code>

i'm not sure whether to use the trace function or not or how to have this thing load my class at all if i run this i get the following as output: TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Ship()
at Main()

thanks for the help guys this is really frustrating me

This topic has been closed for replies.

1 reply

Inspiring
March 9, 2010

What is code in Ship() class - it seems the error originates inside Ship.

Also, your class should be written something like this:

package
{
     import flash.net.URLRequest;
     import flash.display.*;
     import flash.events.*;
     public class Main extends MovieClip
     {
          private var playerShip:Ship = new Ship();
          //var enemyShip = new mc_EnemyShip();
          public function Main()
          {
               addEventListener(Event.ADDED_TO_STAGE, init);
          }
          
          private function init(e:Event):void
          {
               removeEventListener(Event.ADDED_TO_STAGE, init);
               playerShip = new Ship();
               addChild(playerShip);
          }
     }
}

elexionAuthor
Participant
March 16, 2010

my ship class looks like this,

package
{
    import flash.net.URLRequest;
    import flash.display.*;
    import flash.events.*;
   
    public class Ship extends MovieClip
    {     ///the fields my class uses
        public var ship = new mc_PlayerShip();
        public var shipSpeed:int = 5;
        public var leftWall:int = 16;
        public var rightWall:int = 535;       
       
        public var left:Boolean = false;
        public var right:Boolean = false;
        public var up:Boolean = false;
        public var down:Boolean = false;
        public var space:Boolean = false;
        /// the constructor
        public function Ship()
        {  

             addChild(ship);    
            ship.x = 275;
            ship.y = 350;           
           
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeysDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, KeysUp);
            stage.addEventListener(Event.ENTER_FRAME, Update);           
        }
              //my keyboard actions 
        public function KeysDown(event:KeyboardEvent)
        {
            if (event.keyCode == 37)        left = true;
            else if (event.keyCode == 39)    right = true;
            else if (event.keyCode == 38)    up = true;
            else if (event.keyCode == 40)    down = true;
        }
       
        public function KeysUp(event:KeyboardEvent)
        {
            if (event.keyCode == 37)        left = false;
            else if (event.keyCode == 39)    right = false;
            else if (event.keyCode == 38)    up = false;
            else if (event.keyCode == 40)    down = false;
        }
        ///the boundrie collision checks
        public function Update(event:Event)
        {
            if(ship.x < leftWall)
            {
                ship.x = leftWall+1;
            }
            if(ship.x > rightWall)
            {
                ship.x = rightWall+1;
            }
            if (left)
                ship.x -= shipSpeed;
            if (right)
                ship.x += shipSpeed;
            if (up)
                ship.y -= shipSpeed;
            if (down)
                ship.y += shipSpeed;
        }
    }
}

i guess i'm missing a lot of code huh?         

Inspiring
March 16, 2010

One important thing to know about Flash architecture. Stage is available to an object ONLY when object is added to display list. In your case you add event listeners in the constructor - stage IS NOT AVAILABLE to Ship instance at this point. Again, you can access stage when you call addChild.

So, Ship class should be written like this:

package
{
    import flash.net.URLRequest;
    import flash.display.*;
    import flash.events.*;
  
    public class Ship extends MovieClip
    {    
          ///the fields my class uses
          public var ship:mc_PlayerShip;
          public var shipSpeed:int = 5;
          public var leftWall:int = 16;
          public var rightWall:int = 535;      
          public var left:Boolean = false;
          public var right:Boolean = false;
          public var up:Boolean = false;
          public var down:Boolean = false;
          public var space:Boolean = false;
          /// the constructor
          public function Ship()
          { 
               // wait until it is added to stage
               addEventListener(Event.ADDED_TO_STAGE, init());
          }
          
          private function init(e:Event):void
          {
               removeEventListener(Event.ADDED_TO_STAGE, init);
               ship = new mc_PlayerShip();
               addChild(ship);   
               ship.x = 275;
               ship.y = 350;        
               // only now you can write stage related code
               stage.addEventListener(KeyboardEvent.KEY_DOWN, KeysDown);
               stage.addEventListener(KeyboardEvent.KEY_UP, KeysUp);
               stage.addEventListener(Event.ENTER_FRAME, Update);
          }
          //my keyboard actions
          public function KeysDown(event:KeyboardEvent)
          {
          if (event.keyCode == 37) left = true;
          else if (event.keyCode == 39) right = true;
          else if (event.keyCode == 38) up = true;
          else if (event.keyCode == 40) down = true;
          }
      
          public function KeysUp(event:KeyboardEvent)
          {
               if (event.keyCode == 37) left = false;
               else if (event.keyCode == 39) right = false;
               else if (event.keyCode == 38) up = false;
               else if (event.keyCode == 40) down = false;
          }
        ///the boundrie collision checks
          public function Update(event:Event)
          {
               if (ship.x < leftWall) ship.x = leftWall + 1;
               if (ship.x > rightWall) ship.x = rightWall + 1;
               if (left) ship.x -= shipSpeed;
               if (right) ship.x += shipSpeed;
               if (up) ship.y -= shipSpeed;
               if (down) ship.y += shipSpeed;
          }
     }
}