首先,我只想说我正在使用的单例实际上不会在整个应用程序生命周期中都存在。这是一种封装用户正在发生的事情的方法。

我有几个GameState,例如InGame或MainMenu,并且这些函数具有一些非常相似的函数调用,因此我想使用继承来停止复制/粘贴。下面的代码是我所拥有的,但是无法按预期工作。这里是:

BaseState.cs

abstract class BaseState
{
    protected static BaseState mHandle = null;

    protected static BaseState Handle
    {
        get
        {
            return mHandle;
        }
        set
        {
            mHandle = value;
        }
    }

    public static GameState UpdateState(GameTime gameTime)
    {
        GameState g = GameState.MainMenu;

        try
        {
            Handle.Update(gameTime);
        }
        catch (Exception e)
        {

        }

        return g;
    }

    public static void DrawState(GameTime gameTime, SpriteBatch spriteBatch)
    {
        Handle.Draw(gameTime, spriteBatch);
    }

    public static void Release()
    {
        mHandle = null;
    }

    protected abstract GameState Update(GameTime gameTime);
    protected abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);
}


InGame.cs

class InGame : BaseState
{
    private InGame()
    {

    }

    protected static new BaseState Handle
    {
        get
        {
            if (mHandle == null)
            {
                mHandle = new InGame();
            }

            return mHandle;
        }
        set
        {
            mHandle = value;
        }
    }

    protected override GameState Update(GameTime gameTime)
    {
        return GameState.Quit;
    }

    protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {

    }
}


您可能会告诉我,我希望能够在get中使用setInGameBaseState,所以我可以简单地调用Handle.Update(),而无论是从InGame还是Menu中调用它或任何知道要使用哪个代码的信息。

显然,我需要重温我的面向对象技术。但是,如果有人可以提出一种使它完成我想做的事情的方法,或者提出一种不同的解决方法,我将不胜感激。谢谢。

最佳答案

您要实现的目标不需要单例,请参见下文:

public abstract class BaseState
{
    public GameState UpdateState(GameTime gameTime)
    {
        GameState g = GameState.MainMenu;

        try
        {
            g = Update(gameTime); // Update returns a new state
        }
        catch (Exception e)
        {

        }

        return g;
    }

    protected abstract GameState Update(GameTime gameTime);
    protected abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);
}

public class InGame : BaseState
{
    public InGame()
    {

    }

    protected override GameState Update(GameTime gameTime)
    {
        return GameState.Quit;
    }

    protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        // Draw game
    }
}

public class InMenu : BaseState
{
    public InMenu()
    {

    }

    protected override GameState Update(GameTime gameTime)
    {
        return GameState.Pause;
    }

    protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        // Draw menu
    }
}

public void Foo()
{
    List<BaseState> states = new List<BaseState>();
    states.Add(new InGame());
    states.Add(new InMenu());

    // Calls InGame.Update(...)
    states[0].UpdateState(...);

    // Calls InMenu.Update(...)
    states[1].UpdateState(...);
}

10-02 01:46