开发者

Importing External Classes

I'm currently working on a Flash platform game and I'm trying to make each level have its own class that defines a hitTest function (Each class is linked to the MovieClip of the level), which would allow the character to walk on the level. Whenever I try to import the subclass into the Document class, errors start popping up and it is driving me crazy (Error 1120: Access of undefined property)!

Any kind of help would be appreciated!

Document Class (Class_Main.as):

package 
{
    import flash.events.*;
    import flash.display.*;
    import flash.geom.Point;
    import Level1;
    import Level2;

    public class Class_Main extends MovieClip
    {
        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 = 15;
        public var gravityPower:Number = 0.7;
        public var terminalVelocity:Number = 75;
        public var Level_1:Level1 = new Level1();
        public var Level_2:Level2 = new Level2();

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

            while (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y,true))
            {
                mcMain.y--;
            }
            if (! Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 1,true))
            {
                //upKeyDown = false;
                if (! Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 5,true))
                {
                    upKeyDown = false;
                    onGround = false;
                }
            }
            if (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 1,true))
            {
                ySpeed = 0;
                if (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 5,true))
                {
                    onGround = true;
                }
            }
            if (开发者_开发知识库Level_1.wallCollision.hitTestPoint(mcMain.x - 9,mcMain.y - 25,true))
            {
                mcMain.x + 9;
                mcMain.y + 11;
                upKeyDown = false;
                leftKeyDown = false;
            }
            if (Level_1.wallCollision.hitTestPoint(mcMain.x + 9,mcMain.y - 25,true))
            {
                mcMain.x - 9;
                mcMain.y - 11;
                upKeyDown = false;
                rightKeyDown = false;
            }
            if (Level_1.wallCollision.hitTestPoint(mcMain.x - 9,mcMain.y - 11,true))
            {
                xSpeed = 0;
                leftKeyDown = false;
            }
            if (Level_1.wallCollision.hitTestPoint(mcMain.x + 9,mcMain.y - 11,true))
            {
                xSpeed = 0;
                rightKeyDown = false;
            }
            if (Level_1.deathArea.hitTestPoint(mcMain.x,mcMain.y + 1,true))
            {
                mcMain.x = spawnArea.x;
                mcMain.y = spawnArea.y;
            }
            if (mcMain.hitTestObject(Level_1.goalArea.goalHitArea))
            {
                if (stage.contains(Level_1))
                {
                    this.removeChild(Level_1);
                }
                addChild(Level_2);
                Level_2.x = -400;
                Level_2.y = -700;
            }
        }
        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;
        }

    }

}

Level 2 (Level2.as):

package 
{

    import flash.display.MovieClip;
    import flash.events.*;
    import Class_Main;
    import Level2Walls;

    public class Level2 extends MovieClip
    {
        public var classMain:Class_Main = new Class_Main  ;
        public var level2Walls:Level2Walls = new Level2Walls  ;

        public function Level2()
        {
            stage.addEventListener(Event.ENTER_FRAME,hitTest_2);
            // constructor code
        }
        public function hitTest_2(event:Event)
        {
            while (level2Walls.hitTestPoint(mcMain.x,mcMain.y,true))
            {
                mcMain.y--;
            }
            if (! level2Walls.hitTestPoint(mcMain.x,mcMain.y + 1,true))
            {
                //upKeyDown = false;
                if (! level2Walls.hitTestPoint(mcMain.x,mcMain.y + 5,true))
                {
                    upKeyDown = false;
                    onGround = false;
                }
            }
            if (level2Walls.hitTestPoint(mcMain.x,mcMain.y + 1,true))
            {
                ySpeed = 0;
                if (level2Walls.hitTestPoint(mcMain.x,mcMain.y + 5,true))
                {
                    onGround = true;
                }
            }
            if (level2Walls.hitTestPoint(mcMain.x - 9,mcMain.y - 25,true))
            {
                mcMain.x + 9;
                mcMain.y + 11;
                upKeyDown = false;
                leftKeyDown = false;
            }
            if (level2Walls.hitTestPoint(mcMain.x + 9,mcMain.y - 25,true))
            {
                mcMain.x - 9;
                mcMain.y - 11;
                upKeyDown = false;
                rightKeyDown = false;
            }
            if (level2Walls.hitTestPoint(mcMain.x - 9,mcMain.y - 11,true))
            {
                xSpeed = 0;
                leftKeyDown = false;
            }
            if (level2Walls.hitTestPoint(mcMain.x + 9,mcMain.y - 11,true))
            {
                xSpeed = 0;
                rightKeyDown = false;
            }
        }

    }

}


Here are a couple things that could be causing you problems:

  • Level2 shouldn't be creating an instance of Class_Main. There should never be more than one instance of the document class and it is instantiated automatically when you launch your swf.

  • In the Level2 constructor, the reference to stage will be null. You can't get a reference to stage until an object is added to the display list, which can't be done until after the constructor has already run.

  • I don't see anywhere the your objects that are created in your constructor, Level_1 and Level_2, are added to stage. If these items are symbols you've already added on stage in the Flash IDE, then you don't need to create new instances of them in your constructor.

  • You don't need to add import statements if objects are living in the same package.

I don't know that any of these things will clear your errors, but they should at least get you closer.


You need to pass a reference to your main class into the Level classes rather than trying to create a new instance. The easiest way to do that is to pass the reference through the constructor of the level class, and so you shouldn't instantiate the Level classes until you're actually in the Main class constructor:

public class Class_Main extends MovieClip
{
    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 = 15;
    public var gravityPower:Number = 0.7;
    public var terminalVelocity:Number = 75;
    public var Level_1:Level1;
    public var Level_2:Level2;

    public function Class_Main()
    {
        addEventListener(Event.ADDED_TO_STAGE,init);
        Level_1 = new Level1(this);
        Level_2 = new Level2(this);
        // constructor code
    }

and then:

public class Level2 extends MovieClip
{
    public var classMain:Class_Main;
    public var level2Walls:Level2Walls = new Level2Walls;

    public function Level2($ref:Class_Main)
    {
        classMain = $ref;
        stage.addEventListener(Event.ENTER_FRAME,hitTest_2);
        // constructor code
    }

Finally in your hittest function, use classMain to refer to your document class rather than mcMain.

Also, just a side note regarding the names of your Level class instances - you should use a lower case letter for the first letter of an instance or property, ie level_2 rather than Level_2. It's not going to affect your ability to run your program, but it is the AS3 convention, and so a good habit to get into.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜