使子类对象非空

时间:2011-09-21 01:45:45

标签: actionscript-3 flash-cs5

我目前在引用导出时为null的对象时遇到了一些麻烦。基本上我希望Document Class运行另一个类的代码,该类是MovieClip的类,而不会在输出面板中的任何地方弹出 Error 1009

这是我正在尝试运行的子类:

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

    public class McMain extends MovieClip
    {
        public var document:Document;
        public var leftKeyDown:Boolean = false;
        public var rightKeyDown:Boolean = false;
        public var upKeyDown:Boolean = false;
        public var downKeyDown:Boolean = false;
        public var onGround:Boolean = true;
        public var xSpeed:Number = 0;
        public var ySpeed:Number = 0;
        public var mainSpeed:Number = 3.75;
        public var frictionPower:Number = 0.9;
        public var jumpPower:Number = 13;
        public var gravityPower:Number = 0.5;
        public var terminalVelocity:Number = 75;

        public function McMain()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, initMcMain);
            // constructor code
        }
        public function initMcMain(event:Event)
        {
            addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
            addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
            addEventListener(Event.ENTER_FRAME, hitTest);
            addEventListener(Event.ENTER_FRAME, Main);
        }
        public function Main(event:Event):void
        {
            this.moveCharacter();
            this.dynamicMovement();
        }
        public function checkKeysDown(event:KeyboardEvent):void
        {
            if (event.keyCode == 37)
            {
                this.leftKeyDown = true;
            }
            if (event.keyCode == 38)
            {
                this.upKeyDown = true;
            }
            if (event.keyCode == 39)
            {
                this.rightKeyDown = true;
            }
            if (event.keyCode == 40)
            {
                this.downKeyDown = true;
            }
        }
        public function checkKeysUp(event:KeyboardEvent):void
        {
            if (event.keyCode == 37)
            {
                this.leftKeyDown = false;
            }
            if (event.keyCode == 38)
            {
                this.upKeyDown = false;
            }
            if (event.keyCode == 39)
            {
                this.rightKeyDown = false;
            }
            if (event.keyCode == 40)
            {
                this.downKeyDown = false;
            }
        }
        public function moveCharacter():void
        {
            if (this.leftKeyDown)
            {
                this.xSpeed -=  this.mainSpeed;
                if (this.onGround)
                {
                    this.scaleX = -1;
                }
            }
            if (this.rightKeyDown)
            {
                this.xSpeed +=  this.mainSpeed;
                if (this.onGround)
                {
                    this.scaleX = 1;
                }
            }
            if (this.leftKeyDown && this.onGround || this.rightKeyDown && this.onGround)
            {
                this.gotoAndStop(2);
            }
            if (this.currentFrame == 2)
            {
                if (! this.leftKeyDown && ! this.rightKeyDown)
                {
                    this.gotoAndStop(3);
                }
            }
            if (this.currentFrame == 3)
            {
                if (this.skidAnimation.currentFrame == this.skidAnimation.totalFrames)
                {
                    this.gotoAndStop(1);
                }
            }
            if (this.upKeyDown)
            {
                this.ySpeed -=  this.jumpPower;
            }
            if (this.upKeyDown && this.leftKeyDown)
            {
                this.ySpeed -=  0;
                this.xSpeed -=  10;
            }
            if (this.upKeyDown && this.rightKeyDown)
            {
                this.ySpeed -=  0;
                this.xSpeed +=  10;
            }
            if (this.xSpeed > 3 && ! onGround || this.xSpeed < -3 && ! onGround)
            {
                if (this.currentFrame == 2)
                {
                    this.gotoAndStop(5);
                }
            }
            if (this.ySpeed < -0.5 && ! onGround)
            {
                this.gotoAndStop(4);
            }
            else if (this.ySpeed > 0.5 && ! onGround)
            {
                this.gotoAndStop(5);
            }
            if (this.currentFrame == 5 && onGround)
            {
                this.gotoAndStop(1);
            }
            //if (! leftKeyDown && ! rightKeyDown && ! upKeyDown)
            //{
            //mcMain.gotoAndStop(1);
            //}
        }
        public function dynamicMovement():void
        {
            if (onGround)
            {
                this.x +=  this.xSpeed;
                this.xSpeed *=  this.frictionPower;
            }
            else
            {
                this.x +=  this.xSpeed;
                this.xSpeed *=  (this.frictionPower + 0.09);
            }
            if (this.xSpeed > 7)
            {
                this.xSpeed = 7;
            }
            if (this.xSpeed < -7)
            {
                this.xSpeed = -7;
            }
            this.y +=  this.ySpeed;
            this.ySpeed +=  this.gravityPower;
            if (this.ySpeed > this.terminalVelocity)
            {
                this.ySpeed = this.terminalVelocity;
            }
        }
        public function hitTest(event:Event)
        {
            //spawnArea.visible = false;
            this.mcMainHitArea.visible = false;
            this.document.levelChange.wallCollision.visible = false;
            //^^^^^^^^^^^^^^^^^^^^^^^^^^
            //Error 1009 appearing here!
            this.document.levelChange.deathArea.visible = false;
            this.document.levelChange.goalArea.goalHitArea.visible = false;

            while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
            {
                this.y = this.y - 0.5;
                this.ySpeed = 0;
            }
            while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y - 24, true))
            {
                this.y = this.y + 0.5;
                this.ySpeed = 0;
            }
            while (document.levelChange.wallCollision.hitTestPoint(this.x - 12, this.y - 11, true))
            {
                this.x = this.x + 0.5;
                this.xSpeed = 0;
                if (! onGround)
                {
                    this.leftKeyDown = false;
                }
            }
            while (document.levelChange.wallCollision.hitTestPoint(this.x + 12, this.y - 11, true))
            {
                this.x = this.x - 0.5;
                this.xSpeed = 0;
                if (! onGround)
                {
                    this.rightKeyDown = false;
                }
            }
            if (document.levelChange.wallCollision.hitTestPoint(this.x - 16, this.y - 11, true))
            {
                if (this.upKeyDown)
                    {
                        this.ySpeed -=  10;
                        this.xSpeed +=  50;
                    }
            }
            if (document.levelChange.wallCollision.hitTestPoint(this.x + 16, this.y - 11, true))
            {
                if (this.upKeyDown)
                    {
                        this.ySpeed -=  10;
                        this.xSpeed -=  50;
                    }
            }
            if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
            {
                if (this.upKeyDown)
                {
                    this.upKeyDown = false;
                    this.onGround = false;
                }
            }
            if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
            {
                if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1.5, true))
                {
                    this.upKeyDown = false;
                }
                if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
                {
                    //upKeyDown = false;
                    onGround = false;
                }
            }
            if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
            {
                this.ySpeed = 0;
                if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
                {
                    onGround = true;
                }
            }
            if (document.levelChange.deathArea.hitTestPoint(this.x, this.y + 1, true))
            {
                this.x = this.x;
                this.y = this.y;
            }
            if (this.hitTestObject(document.levelChange.goalArea.goalHitArea))
            {
                if (stage.contains(document.level_1))
                {
                    this.removeChild(document.level_1);
                    //stage.removeEventListener(Event.ENTER_FRAME,hitTest);
                }
                if (stage.contains(document.spawnArea))
                {
                    this.x = this.x;
                    this.y = this.y;
                }
                addChild(document.level_2);
                document.level_2.x = -1425;
                document.level_2.y = -2550;
            }
        }

    }

}

文件类:

package 
{
    import flash.events.*;
    import flash.display.*;
    import flash.geom.Point;
    import McMain;
    import RestartButton;
    import Level_2;

    public class Document extends MovieClip
    {
        public var levelNumber:int = 1;
        public var levelChange:Object;
        public var levelArray:Array = new Array();
        public var collisionArray:Array = new Array();
        public var deathAreaArray:Array = new Array();
        public var goalAreaArray:Array = new Array();
        public var goalHitAreaArray:Array = new Array();
        public var mcMain:McMain;
        public var restartButton:RestartButton;
        public var level_2:Level_2;

        public function Document()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
            mcMain = new McMain();
            mcMain.document = this;
            restartButton = new RestartButton();
            restartButton.document = this;
            level_2 = new Level_2();
            // constructor code
        }
        public function init(event:Event)
        {
            this.levelChange = this.level_1;
        }
        public function levelHandler(event:Event)
        {
            this.levelChange = this["level_" + levelNumber];
            if (level_2.stage)
            {
                levelNumber = 2;
                //trace(levelChange);
            }
            for (var i:int = numChildren - 1; i >= 0; i--)
            {
                var collisionChild:DisplayObject = getChildAt(i);
                if (collisionChild.name == "wallCollision")
                {
                    collisionArray.push(collisionChild);
                }
                var deathAreaChild:DisplayObject = getChildAt(i);
                if (deathAreaChild.name == "deathArea")
                {
                    deathAreaArray.push(deathAreaChild);
                }
                var goalAreaChild:DisplayObject = getChildAt(i);
                if (goalAreaChild.name == "goalArea")
                {
                    goalAreaArray.push(goalAreaChild);
                }
            }
            for (var i_2:int = numChildren - 2; i >= 0; i--)
            {
                var goalHitAreaChild:DisplayObject = getChildAt(i_2);
                if (goalHitAreaChild.name == "goalHitArea")
                {
                    goalHitAreaArray.push(goalHitAreaChild);
                }
            }
        }
        public function vCamMovement(event:Event):void
        {
            /*for (var i:int = 0; i < this.numChildren - 1; i++)
            {
            this.getChildAt(i).x -=  xSpeed;
            //levelObjects.getChildAt(i).y -=  ySpeed;
            }*/
            level_1.x +=  stage.stageWidth * 0.5 - mcMain.x;
            level_1.y +=  stage.stageHeight * 0.5 - mcMain.y;
            level_2.x +=  stage.stageWidth * 0.5 - mcMain.x;
            level_2.y +=  stage.stageHeight * 0.5 - mcMain.y;
            spawnArea.x +=  stage.stageWidth * 0.5 - mcMain.x;
            spawnArea.y +=  stage.stageHeight * 0.5 - mcMain.y;
            mcMain.x = stage.stageWidth * 0.5;
            mcMain.y = stage.stageHeight * 0.5;
        }

    }

}

编辑:修复了代码中的一个小错误,但代码仍无效。

编辑:

这是调试器输出:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at McMain/hitTest()[C:\Users\*\Desktop\*\Flash\*\McMain.as:186] *= XXXXXXXXXXXX

1 个答案:

答案 0 :(得分:0)

两件事。 1,我无法看到你初始化level_1对象的任何地方。第二,你应该将这段代码从构造函数对象中移出到init方法中。

mcMain = new McMain();
mcMain.document = this;
restartButton = new RestartButton();
restartButton.document = this;

原因很简单。您只是创建mcMain,然后将其.document属性设置为“this”。在mcMain中,您引用了mcMain.document.levelChange,但是直到稍后才在init方法中定义document.levelChange。我并不完全相信这绝对是你的问题,但是当你在动态类上访问动态属性时,你真的应该做错误检查。尝试做像

这样的事情
if(document != null && document.levelChange != null)
在您尝试使用hitTest显式引用这些属性/对象之前