有没有办法让其他类识别另一个类中的公共变量



我使用的编码语言是带有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())
        }

    }
}

您可能需要做的是:

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

}

相关内容

  • 没有找到相关文章

最新更新