最初に、アプリの存続期間全体を通して実際には存在しないシングルトンを使用していることを述べます。これは、ユーザーから何が起こっているかをカプセル化する方法です。
InGameやMainMenuなどのいくつかのGameStateがあり、これらには非常によく似た関数呼び出しがあるため、継承を使用してコピー/貼り付けを停止することを考えました。次のコードは私が持っているものですが、意図したとおりに機能しません。ここにあります:
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
おそらく、とset
を使用できるようにしたいのでInGame
、BaseState
Handle.Update()を呼び出すだけで、InGameやMenuから呼び出した場合でも、使用するコードがわかっている場合でも、を使用できるようになります。
明らかに、私は自分のOOスキルを磨く必要があります。しかし、誰かがこれを私がやりたいことをするための方法を提案したり、それについて別の方法を提案したりすることができれば、私は感謝するでしょう。ありがとう。