您需要将游戏类的引用传递给 Configs 类。在 Configs 的类构造函数中,为游戏类的实例设置一个私有成员变量。从那里你可以操纵游戏。见下文:
游戏课
public class Game1 : Game
{
Configs configs;
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Pass THIS game instance to the Configs class constructor.
configs = new Configs(this);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
配置类
public class Configs
{
// Reference to the main game class.
private Game _game;
public Configs(Game game)
{
// Store the game reference to a local variable.
_game = game;
}
public void SetMouseVisible()
{
// This is a reference to you main game class that was
// passed to the Configs class constructor.
_game.IsMouseVisible = true;
}
}
这基本上是其他人一直在说的。但是,您似乎很难在没有看到一些代码的情况下理解这些概念。因此,我提供了它。
希望有帮助。