开发者

Making Subclass Objects Non-Null

开发者 https://www.devze.com 2023-04-06 12:25 出处:网络
I\'m currently having some trouble with referencing objects that turn out null on export. Basically I want the Document Class to run the code of another class that is the class of a MovieClip without

I'm currently having some trouble with referencing objects that turn out null on export. Basically I want the Document Class to run the code of another class that is the class of a MovieClip without having Error 1009 popping up everywhere in my output panel.

Here's the subclass I'm trying to run:

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))
            {
                t开发者_运维问答his.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;
            }
        }

    }

}

Document Class:

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;
        }

    }

}

Edit: Fixed a small typo within the code, but the code is still not working.

Edit:

Here's the debugger output:

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


Two things. 1st, I cannot see anywhere that you initialize level_1 object. Two, you should move this code out of your constructor object and into your init method.

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

The reason is simple. You're just creating mcMain, then setting its .document property to "this." Within mcMain, you're referencing mcMain.document.levelChange, but you do not define document.levelChange until later, in your init method. I'm not entirely convinced that this is absolutely your issue, but you really should be do doing error checking when you're accessing dynamic properties on a dynamic class. Try doing things like

if(document != null && document.levelChange != null)

before you attempt to explicitly reference those properties/objects as you are with your hitTest.

0

精彩评论

暂无评论...
验证码 换一张
取 消