0

我正在创建一个平台游戏。但是,我在平台上创建碰撞边界以使玩家在平台上跳跃而不掉落后遇到错误。

我创建了一个矩形框并将其导出为platForm

这是错误的输出:错误一遍又一遍地重复自己......

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Boy/BoyMove()

主类:

package 
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.*;

public class experimentingMain extends MovieClip 
{
    var count:Number = 0;
    var myTimer:Timer = new Timer(10,count);

    var classBoy:Boy;



    //var activateGravity:gravity = new gravity();

    var leftKey, rightKey, spaceKey, stopAnimation:Boolean;

    public function experimentingMain() 
    {
        myTimer.addEventListener(TimerEvent.TIMER, scoreUp);
        myTimer.start();



        classBoy = new Boy();
        addChild(classBoy);

        stage.addEventListener(KeyboardEvent.KEY_DOWN, pressTheDamnKey);
        stage.addEventListener(KeyboardEvent.KEY_UP, liftTheDamnKey);
    }

    public function pressTheDamnKey(event:KeyboardEvent):void
    {
        if (event.keyCode == 37)
        {
            leftKey = true;
            stopAnimation = false;
        }

        if (event.keyCode == 39)
        {
            rightKey = true;
            stopAnimation = false;
        }

        if (event.keyCode == 32)
        {
            spaceKey = true;
            stopAnimation = true;
        }
    }

    public function liftTheDamnKey(event:KeyboardEvent):void
    {
        if (event.keyCode == 37)
        {
            leftKey = false;
            stopAnimation = true;
        }

        if (event.keyCode == 39)
        {
            rightKey = false;
            stopAnimation = true;
        }

        if (event.keyCode == 32)
        {
            spaceKey = false;
            stopAnimation = true;
        }
    }

    public function scoreUp(event:TimerEvent):void 
    {
        scoreSystem.text = String("Score : "+myTimer.currentCount);
    }

}
   }

男生班:

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

public class Boy extends MovieClip
{
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    //the main character's speed
    var mainSpeed:Number = 15;
    //whether or not the main guy is jumping
    //var mainJumping:Boolean = false;
    var mainJumping:Boolean = false;
    //how quickly should the jump start off
    var jumpSpeedLimit:int = 40;
    //the current speed of the jump;
    var jumpSpeed:Number = 0;
    var gravity:Number = 10;

    var theGround:ground = new ground();


    //var theCharacter:MovieClip;

    public var currentX,currentY:int;

    public function Boy()
    {
        this.x = 600;
        this.y = 540;

        addEventListener(Event.ENTER_FRAME, BoyMove);
    }

    public function BoyMove(event:Event):void
    {
        currentX = this.x;
        currentY = this.y;

        if (MovieClip(parent).leftKey)
        {
            currentX -=  mainSpeed;
            MovieClip(this).scaleX = 1;
        }

        if (MovieClip(parent).rightKey)
        {
            currentX +=  mainSpeed;
            MovieClip(this).scaleX = -1;
        }


        if (MovieClip(parent).spaceKey || mainJumping)
        {
            mainJump();
        }

        this.x = currentX;
        this.y = currentY;
    }

    public function mainJump():void
    {
        currentY = this.y;


        if (! mainJumping)
        {

            mainJumping = true;
            jumpSpeed = jumpSpeedLimit * -1;
            currentY +=  jumpSpeed;
        }
        else
        {
            if (jumpSpeed < 0)
            {
                jumpSpeed *=  1 - jumpSpeedLimit / 250;
                if (jumpSpeed > -jumpSpeedLimit/12)
                {
                    jumpSpeed *=  -2;
                }
            }
        }
        if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
        {
            jumpSpeed *=  1 + jumpSpeedLimit / 120;
        }
        currentY +=  jumpSpeed;

        if (MovieClip(this).y > 500)
        {
            mainJumping = false;
            MovieClip(this).y = 500;
        }



        this.y = currentY;
    }
}
    }

Platformer 类:这是我要为矩形设置边界的类(platForm)

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

public class platForm extends MovieClip
{
    var level:Array = new Array();
    var classBoys:Boy = new Boy();
    var speedx:int = MovieClip(classBoys).currentX;

    public function platForm() 
    {

        for (var i = 0; i < numChildren; i++)
        {
            if (getChildAt(i) is platForm) 
            {
                level.push(getChildAt(i).getRect(this));
            }
        }

        for (i = 0; i < level.length; i++) 
        {
            if (MovieClip(classBoys).getRect(this).intersects(level[i]))
            {
                if (speedx > 0) 
                {
                    MovieClip(classBoys).x = level[i].left - MovieClip(classBoys).width/2;
                }
                if (speedx < 0) 
                {
                    MovieClip(classBoys).x = level[i].right - MovieClip(classBoys).width/2;
                }
            }
        }
    }
}
 }
4

1 回答 1

0

如果无法运行您的代码,很难确切地看到正在发生的事情,但错误是说您的 BoyMove() 方法中的某些内容正在尝试引用 null 的某个属性(或方法)。查看 BoyMove() 方法后,我可以看到没有太多可能导致此问题的方法。另外两名候选人将是

MovieClip(parent)

或者

MovieClip(this)

您正在尝试访问这两个 MovieClip 的属性。其中之一不得按您的预期初始化。我建议您通过使用 MovieClip(parent) 注释掉这些行来对该方法进行一些基本调试,看看是否仍然出现错误。然后用 MovieClip(this) 的行尝试相同的操作。这应该足以隔离问题。

于 2013-02-13T16:15:40.360 回答