是否有其他类可以识别另一个类中的公共变量?

时间:2016-06-02 21:55:10

标签: actionscript-3 global-variables

我使用的编码语言是带有Flash Develop的AS3。我想要做的是允许类章节访问类主菜单中的公共变量。该变量名为YesNo,当我尝试在类章节中访问它时,它不起作用。我下载了一些名为Flashpunk的东西,它带有多个不同类的预设,有助于制作游戏,我将在下面显示我的代码。

主菜单

package 
{
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;
    import net.flashpunk.FP;

    public class MainMenu extends Entity
    {

        [Embed(source = "net/MainScreen.png")]
        private const SPRITE1:Class;

        private var sprite1:Image = new Image(SPRITE1);

        public var YesNo:int = 0

        public function MainMenu() 
        {
            graphic = sprite1;
            sprite1.centerOrigin();

            x = 200
            y = 150

            layer = 150
        }


        override public function update():void
        {
            if (Input.pressed(Key.DIGIT_1))
            {
                YesNo = YesNo + 1
            }

            if (Input.pressed(Key.DIGIT_2))
            {
                YesNo = YesNo + 2
            }
        }




    }

}

package 
{
    import net.flashpunk.Entity;
    import net.flashpunk.World;

    public class Chapter extends World
    {

        public function Chapter() 
        {
            add(new MainMenu())

        }


    }

}

1 个答案:

答案 0 :(得分:0)

您可能需要做的是:

public class Chapter extends World
{
    //create a class level var that you can access anywhere in this class
    public var mainMenu:MainMenu;

    public function Chapter() 
    {
        //instantiate your menu class and assign it to the class level var
        mainMenu = new MainMenu();

        add(mainMenu);

        //access the menu's public properties and functions like this:
        mainMenu.YesNo = 0; //or whatever you need to do with that var.
    }


}