为什么我不能从其他类访问公共变量?

时间:2017-02-19 22:29:56

标签: actionscript-3 flash actionscript air

我想在我的游戏文档类中定义我的变量,然后在我的Movement类中使用其中的一些变量。但是,当我在我的运动类中使用这些变量时,我得到了大量的编译错误,说它们都是未定义的。

因此我的问题是,为什么我的公共变量没有传递给我的其他类?我通过import game;导入了这个类,所以这让我很困惑。我可能只是做错了,但非常感谢帮助。

时间轴

addChild((new Movement));

游戏文档类

package 
{
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class game extends MovieClip
    {
        public var area1:Boolean = true;
        public var area2:Boolean = false;
        public var area3:Boolean = false;

        public var player1:Boolean = true;

        public var playerPosKeeper_mc:MovieClip = new mc_PlayerPosKeeper();

        public var up_dpad:MovieClip = new dpad_Up();
        public var down_dpad:MovieClip = new dpad_Down();
        public var left_dpad:MovieClip = new dpad_Left();
        public var right_dpad:MovieClip = new dpad_Right();
        public var menu_dpad:MovieClip = new dpad_Menu();
        public var run_dpad:MovieClip = new dpad_Menu();

        public var barrierRoof1_game:MovieClip = new game_BarrierRoof();
        public var barrierRoof2_game:MovieClip = new game_BarrierRoof();
        public var barrierSide1_game:MovieClip = new game_BarrierSide();
        public var barrierSide2_game:MovieClip = new game_BarrierSide();

        public function game()
        {
            trace("SUCCESS | Constructed Game Class");

        }
    }
}

运动类

package 
{
    import game;
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.TouchEvent;
    import flash.net.dns.AAAARecord;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;


    public class Movement extends MovieClip
    {
        var inMotion:Boolean = false;

        public function Movement()
        {
            trace("SUCCESS | Constructed Movement Class");

            addChild(playerPosKeeper_mc);
            playerPosKeeper_mc.x = 384;
            playerPosKeeper_mc.y = 46;

            addChild(up_dpad);
            up_dpad.x = 55;
            up_dpad.y = 336;

            addChild(down_dpad);
            down_dpad.x = 57;
            down_dpad.y = 432;

            addChild(left_dpad);
            left_dpad.x = 19;
            left_dpad.y = 372;

            addChild(right_dpad);
            right_dpad.x = 118;
            right_dpad.y = 372;

            addChild(menu_dpad);
            menu_dpad.x = 61;
            menu_dpad.y = 377;

            addChild(run_dpad);
            run_dpad.x = 684;
            run_dpad.y = 369;

            addChild(barrierRoof1_game);
            barrierRoof1_game.x = 0;
            barrierRoof1_game.y = 0;

            addChild(barrierRoof2_game);
            barrierRoof2_game.x = 0;
            barrierRoof2_game.y = 470;

            addChild(barrierSide1_game);
            barrierSide1_game.x = 0;
            barrierSide1_game.y = 0;

            addChild(barrierSide2_game);
            barrierSide2_game.x = 790;
            barrierSide2_game.y = 0;

            Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

            up_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, moveUpTouchBEGIN);
            up_dpad.addEventListener(TouchEvent.TOUCH_END, moveUpTouchEND);

        }
        public function moveUpTouchBEGIN(e:TouchEvent):void
        {
            trace("Touch Began")
        }

        public function moveUpTouchEND(e:TouchEvent):void
        {
            trace("Touch Ended")
        }
    }

}

1 个答案:

答案 0 :(得分:3)

你很困惑,因为“公共”并不意味着“全球”。将变量声明为公共意味着您希望授予对任何实体的访问权限,前提是实体知道 where 该变量。

让我们看看你有什么。

  • 顶部显示对象:舞台。
  • 主要时间表:游戏是舞台的孩子。
  • MovieClips up_dpad,down_dpad等等,都是游戏实例的字段。
  • 运动:实例是游戏的孩子。

在运动中,您尝试访问up_dpad和其他人,但它们被保存为游戏实例的字段,与Movement实例无关。这就是为什么他们不属于运动的解决背景。

因此,如果您希望将这些变量声明为游戏字段,则需要以正确的方式从运动中解决这些变量。

忘记时间表。在游戏中(为了清楚起见,除非在枪口下,否则永远不会以小写形式调用类),我相信,这是一个根文档类:

    public function Game()
    {
        trace("SUCCESS | Constructed Game Class");

        // Pass reference to this instance to the constructor.
        var aMove:Movement = new Movement(this);

        // Add it as a child.
        addChild(aMove);
    }

在运动中:

    public function Movement(main:Game)
    {
        trace("SUCCESS | Constructed Movement Class");

        // Access variables inside Game instance via given reference.
        addChild(main.playerPosKeeper_mc);
        main.playerPosKeeper_mc.x = 384;
        main.playerPosKeeper_mc.y = 46;

        // ...and so on
    }

这些是面向对象编程的基础知识。如果要访问对象内的任何内容,则需要首先访问该对象。