0

回复谢谢@Corak

我会保持简短,因为我倾向于胡扯并偏离主题。我正试图从另一个类退出我的 XNA 游戏,我的游戏引擎中的菜单类,无论我尝试什么,它都不会退出。我以为是我使用的屏幕管理器,但在创建一个新项目后,它原来是我。

据我了解,Game1.cs 继承自 XNA 游戏类并从中获取退出函数(如果我将鼠标悬停在空白项目中的退出函数上,它显示为 Game.Exit)。

因此,对于我来说,要访问同一个电话,我会让我的班级像这样成为游戏班级的一部分。

public class MainMenu : Game

我在我的游戏引擎中也尝试了 Game1,但仍然是相同的结果,它被调用但没有退出,但这会由于某种原因导致空白项目中的堆栈溢出。然后访问我刚刚调用的退出函数

Exit();

与 Game1.cs 中的相同,但它根本不退出,那么我在这里看不到的基本内容是什么?

在我看来,如果方法/功能可供我查看和调用,为什么它不这样做?编译器和智能感知非常擅长给你错误,所以如果我需要改变类,因为某些东西没有正确继承,为什么不这么说。如果我可以看到所有内容,那么我肯定可以使用它吗?

请让我摆脱痛苦。

问候

德尔

编辑:

我似乎不太擅长解释我想要什么,所以这里有完整的代码来看看我想要做什么,菜单更新方法中的所有内容我都可以在 game1 更新中正常运行,但我需要从菜单类。

我的游戏1.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;
}
}

}
4

0 回答 0