tags:

views:

49

answers:

2

Creating a pacman-like game, where a player eats objects. When a MovieClip (box) eats a circle on the stage, the score in the dynamic text field should go up by 10 points.

Error message:

    Cannot access a property or method of a null object reference.  
    at Move/init()  
at Move()


package {
   import flash.display.Sprite;
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.ui.Keyboard;

   public class Move extends MovieClip {

   //var ScoreObjects:Array = new Array(); // creates ScoreObjects array
      var circle:MovieClip;


      private var keyRight:Boolean=false;
      private var keyLeft:Boolean=false;
      private var keyForward:Boolean=false;
      private var keyBackward:Boolean=false;
      private var forwardMove:int=0;
      private var sideMove:int=0;

      private var inertia:int=8; //amount of friction

   //var score_field:String;
   //var point:MovieClip;
   //private var playerScore:int;

   var currentScore:int;


 // Constructor--------------------------------------------------------------------
      public function Move() {
         init();
      }

 // function init -----------------------------------------------------------------
   function init():void {

         //stage.frameRate=60;
    //var score_field:String="";

  /*ScoreObjects[0] = new Circle();
  ScoreObjects[0].amount = 1; // amount of point -- not sure
  ScoreObjects[0].name = "circle";*/
  circle.amount = 10; // each circle is worth 10 points

  //var playerScore:int = 0;
  var currentScore:int = 0;

         stage.addEventListener(Event.ENTER_FRAME, frameloop);
         stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
         stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

    box.addEventListener(Event.ENTER_FRAME, eatCircle);
    wall.addEventListener(Event.ENTER_FRAME, hitWall);



    //stage.addChild(ScoreObjects[0]); // add Score Objects to stage ------------------------------
    //trace(ScoreObjects[0]);

    /*ScoreObjects[0].x = 105;
    ScoreObjects[0].y = 233;*/

      }

 // function eatCircle --------------------------------------------------------------
 function eatCircle(event:Event):void {

  if (box.hitTestObject(circle)) {
    trace ("I ate the circle");
    stage.removeChild(circle);
    var newPoint:int;
    newPoint = circle.amount;
    // point = ScoreObjects[0].amount //store property's value of amount in variable...?
    calcPoints(newPoint);
    //box.deleteEventListener(Event.ENTER_FRAME, eatCircle)
    //calcScore();
    //playerScore++;
   } else {
    trace ("I didn't eat the circle");
   }
 }

 function calcPoints(newPoint:int):void {
  currentScore += newPoint;
  updateDisplayScore(currentScore); 
 }

 function updateDisplayScore(currentScore:int) {
   scoreField.text = " " + String(currentScore);
   var displayText:String="";   
  displayText = scoreField.text; // assigning the message to the field*/
 }



 // function hitWall --------------------------------------------------------------
 function hitWall(event:Event):void {
  if (box.hitTestObject(wall)) {
    box.y+=6;
   } else if (box.hitTestObject(wall2)) {
    box.y-=6;

   } else if (box.hitTestObject(wall3)) { 
    box.x+=6;

   } else if (box.hitTestObject(wall4)) {
    box.x-=6;
      }

 }

 // function keyDownEvent ------------------------------------------------------------
     function keyDownEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {
            case Keyboard.UP:
               keyForward = true;
               keyBackward = false;
               break;

            case Keyboard.DOWN:
               keyBackward = true;
               keyForward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = true;
               keyRight = false;
               break;

            case Keyboard.RIGHT:
               keyRight = true;
               keyLeft = false;
               break;
         }
      }

 // function keyUpEvent ------------------------------------------------------------
     function keyUpEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {

            case Keyboard.UP:
               keyForward = false;
               break;

            case Keyboard.DOWN:
               keyBackward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = false;
               break;

            case Keyboard.RIGHT:
               keyRight = false;
               break;
         }
      }

 // function frameloop ------------------------------------------------------------
     function frameloop(event:Event):void {

         //amount of force
         if (keyForward) {
            forwardMove -=2;
         }
         if (keyBackward) {
            forwardMove += 2;
         }
         if (keyLeft) {
            sideMove -= 2;
         }
         if (keyRight) {
            sideMove += 2;
         }

         forwardMove +=(0-forwardMove)/inertia;
         sideMove +=(0-sideMove)/inertia;

         box.y+=forwardMove;
         box.x+=sideMove;

   //point = e.currentTarget.amount;


      } 

   }// end of class
}// end of package
+1  A: 

First you're trying to access your circle variable it even though it's not initialized yet (e.g. new MovieClip()), and you're also trying to access an amount property which does not exist in the MovieClip class.

You would need to create a new Circle class that extends the MovieClip class and add a public amount property for that to work.

louisroy
A: 

Looks like one of your properties that you are using in your init method is not set yet.

Try putting a trace before each line of code in the init method. This will tell you at what line it is breaking.

If "circle", "box" and "wall" are movieclips on the timeline then you should access them using getChildByName('circle'); etc.

TandemAdam