答えてくれてありがとう@Corak
私はワッフルしてトピックから外れる傾向があるので、これを短くします. ゲーム エンジンのメニュー クラスの別のクラスから XNA ゲームを終了しようとしていますが、何を試しても終了しません。私が使用していたスクリーンマネージャーだと思っていましたが、新しいプロジェクトを作成した後、それは私であることが判明しました。
私が理解しているように、Game1.cs は XNA ゲーム クラスから継承され、そこから終了関数を取得します (空白のプロジェクトで終了関数の上にマウスを置くと、Game.Exit と表示されます)。
したがって、同じ呼び出しにアクセスするには、クラスをゲームクラスの一部にします。
public class MainMenu : Game
ゲーム エンジンでも Game1 を試しましたが、同じ結果が呼び出されますが終了しませんが、何らかの理由で空白のプロジェクトでスタック オーバーフローが発生します。次に、先ほど呼び出した exit 関数にアクセスします
Exit();
Game1.cs と同じですが、単に終了しないので、ここで見落としている基本的なことは何ですか?
私の目には、メソッド/関数を表示して呼び出すことができる場合、なぜそれを行わないのですか? コンパイラとインテリセンスはエラーを出すのに非常に優れているため、クラスを変更する必要がある場合、適切に継承されていないものとして、なぜそう言わないのですか。全部見れたらきっと使えるよね?
私を不幸から解放してください。
よろしく
デル
編集:
私が何を望んでいるのかを説明するのがあまり得意ではないようですので、ここに私がやろうとしていることを確認するための完全なコードがあります。メニュークラス。
私のGame1.cs
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Create my menu
MainMenu mainMenu = new MainMenu();
SpriteFont MenuFont;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
MenuFont = Content.Load<SpriteFont>("Quartz MS");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// Update Menu
mainMenu.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
mainMenu.Draw(gameTime, spriteBatch, 800, 500, MenuFont);
spriteBatch.End();
base.Draw(gameTime);
}
}
マイメニュー.cs
public class MainMenu : Game
{
// List to store menu items in
private List<string> MenuItems;
// Set some values so we can slow down our menu screen input
float keyPressCheckDelay = 0.30f;
float totalElapsedTime = 0.0f;
private KeyboardState keyboardState;
private int option;
public int Option
{
get { return option; }
set { option = value;
if (option > MenuItems.Count - 1) option = MenuItems.Count - 1;
if (option < 0) option = 0;
}
}
public MainMenu()
{
MenuItems = new List<string>();
MenuItems.Add("Enter Game");
MenuItems.Add("Quit");
Option = 0;
}
public int GetNumberOfOptions()
{
return MenuItems.Count;
}
public string GetItem(int index)
{
return MenuItems[index];
}
public new void Update(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
totalElapsedTime += elapsed;
// Update our Keyboard and gamepad states
keyboardState = Keyboard.GetState();
if (totalElapsedTime >= keyPressCheckDelay)
{
if (keyboardState.IsKeyDown(Keys.Down))
{
Option++;
totalElapsedTime = 0.0f;
}
else if (keyboardState.IsKeyDown(Keys.Up) )
{
Option--;
totalElapsedTime = 0.0f;
}
else if (keyboardState.IsKeyDown(Keys.Enter))
{
if (Option == 0)
{
totalElapsedTime = 0.0f;
Option = 0;
}
if (Option == 1)
{
// how to exit game here ?
Exit();
totalElapsedTime = 0.0f;
Option = 0;
}
}
}
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, int screenWidth, int screenHeight, SpriteFont menuFont)
{
int yPos = 100;
for (int i = 0; i < GetNumberOfOptions(); i++)
{
Color colour = Color.White;
if (i == Option)
{
colour = Color.Goldenrod;
}
spriteBatch.DrawString(menuFont, GetItem(i), new Vector2(screenWidth / 2 - menuFont.MeasureString(GetItem(i)).X / 2, yPos), colour);
yPos += 25;
}
}
}