无法访问null对象引用的属性或方法。 (AS3)

时间:2010-09-22 22:52:36

标签: flash

创建一个类似pacman的游戏,玩家在这里玩物体。当MovieClip(方框)在舞台上吃圆圈时,动态文本字段中的分数应该上升10个点。

错误讯息:

    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

2 个答案:

答案 0 :(得分:1)

首先,您尝试访问 circle 变量,即使它尚未初始化(例如新的MovieClip()),并且您还尝试访问金额属性,它不存在于MovieClip类中。

您需要创建一个新的Circle类,该类扩展MovieClip类并为其添加公共 amount 属性。

答案 1 :(得分:0)

看起来您在init方法中使用的某个属性尚未设置。

尝试在init方法的每行代码之前放置一条跟踪。这将告诉你它正在破坏的线路。

如果“circle”,“box”和“wall”是时间轴上的动画片段,那么您应该使用getChildByName('circle');等访问它们。