在 XNA C# 中无法从外部类访问公共枚举



我有一个类GameServices,其中包含类GameStates的实例。
类 GameStates 有一个名为 GameState 的公共枚举和一个名为 CurrentGameState 的静态变量。

我的问题是,我可以从GameStates类访问CurrentGameState,但不能访问GameState枚举本身,因为它是公开的。

游戏服务类

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using SpaceGame.UI;
using SpaceGame.Content;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace SpaceGame.Game
{
    public class GameServices
    {
        public static ContentLoader ContentLoaderService;
        public static UIHandler UIHandlerService;
        public static GameStates GameStatesService;
        public static void Initialize(ContentManager contentManager, GraphicsDevice graphicsDevice)
        {
             ContentLoaderService = new ContentLoader(contentManager);
             UIHandlerService = new UIHandler(graphicsDevice);
             GameStatesService = new GameStates();
        }
    }
}

游戏状态类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SpaceGame.Game
{
    public class GameStates
    {
        public GameState CurrentGameState { get; set; }
        public enum GameState
        {
            Settings,
            Splash,
            SpaceShipyard
        }
        public GameStates()
        {
            CurrentGameState = GameState.Splash;
        }
    }
}

我做错了什么吗?

提前致谢,
Mark

您应该能够访问它。由于已在 GameStates 类中定义了它,因此该类型的全名将是:

SpaceGame.Game.GameStates.GameState

如果这仍然没有解决,那么您可能会遇到命名空间问题,这应该始终有效:

global::SpaceGame.Game.GameStates.GameState

如果您尝试从同一命名空间访问它,则必须执行以下操作

GameStates.GameState state = GameStates.GameState.Settings

如果你正在这样做,但它不起作用,那么我现在无法帮助你。

这是我做的一个示例。 它为我编译。

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            A.AEnum a = A.AEnum.a;
        }
    }
    public class A
    {
        public enum AEnum
        {
            a,b
        }
    }
}

基本上,不要在其他类中使用枚举(或类)。使用它们时有很多并发症。

此外,CurrentGameState 不是静态的(至少在你提供的代码中),你说它应该是静态的,所以改变它应该有效。

最新更新