导入外部类

时间:2011-08-20 15:23:12

标签: actionscript-3 flash-cs5

我目前正在开发一个Flash平台游戏,我正在尝试让每个级别都有自己的类来定义一个hitTest函数(每个类都链接到该级别的MovieClip),这将允许角色走在水平上。每当我尝试将子类导入Document类时,错误就会弹出,这让我发疯(错误1120:访问未定义的属性)!

任何形式的帮助都将不胜感激!

文档类(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;
        }

    }

}

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

    }

}

2 个答案:

答案 0 :(得分:1)

以下是一些可能导致问题的事情:

  • Level2不应该创建Class_Main的实例。应该永远不会有多个文档类实例,并且在启动swf时会自动实例化。

  • 在Level2构造函数中,对stage的引用将为null。在将对象添加到显示列表之前,您无法获得对stage的引用,这在构造函数已经运行之后才能完成。

  • 我没有看到在构造函数中创建的对象Level_1和Level_2被添加到舞台中的任何位置。如果这些项目是您已在Flash IDE中添加到舞台上的符号,则无需在构造函数中创建它们的新实例。

  • 如果对象位于同一个包中,则无需添加import语句。

我不知道任何这些事情都会清除你的错误,但它们至少应该让你更接近。

答案 1 :(得分:0)

您需要将对主类的引用传递给Level类,而不是尝试创建新实例。最简单的方法是通过级别类的构造函数传递引用,因此在实际处于Main类构造函数之前,不应实例化Level类:

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
    }

然后:

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
    }

最后在您的hittest函数中,使用classMain来引用您的文档类而不是mcMain

另外,只是关于Level类实例名称的旁注 - 您应该使用小写字母作为实例或属性的第一个字母,即level_2而不是Level_2。它不会影响你运行程序的能力,但它是AS3惯例,因此是一个很好的习惯。