我知道在主Game1()构造函数中假设图形设备声明的方式是:
GraphicsDeviceManager graphics;
graphics = new GraphicsDeviceManager(this);
然后您可以使用以下内容:
graphics.PreferredBackBufferWidth = 1366;
但是如果我在一个单独的类中声明相同的内容,我会为“this”填写什么?
GraphicsDeviceManager graphics;
graphics = new GraphicsDeviceManager(?);
编辑:
按照你说的那样修改了所有内容之后,我现在收到一条错误,它将我发送到这行代码:
/// <summary>
/// Event handler for when the Play Game menu entry is selected.
/// </summary>
void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e, Game game)
{
LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
new GameplayScreen(game));
}
此程序是您可以从Microsoft获得的菜单示例,当然经过大量修改,这是当您在主菜单屏幕上按Enter键并且突出显示“Play Game”时执行的代码。我想问题是传递变量。
编辑2:
我修改了我认为的代码,但现在它将我发送到这一行,我不知道如何编辑它。
playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
答案 0 :(得分:1)
您需要引用您的Game实例。我只是将它传递给你的新类构造函数。
例如,如果您想将GraphicsDeviceManager
实例移动到单独的类Foo
,
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
private Foo _foo;
public Game1()
{
_foo = new Foo(this);
}
protected override void Dispose(bool disposing)
{
if (_foo != null)
{
_foo.Dispose();
_foo = null;
}
base.Dispose(disposing);
}
}
public class Foo : IDisposable
{
private Game _game;
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Foo(Game game)
{
_game = game;
_game.Content.RootDirectory = "Content";
_graphics = new GraphicsDeviceManager(_game);
_spriteBatch = new SpriteBatch(_game.GraphicsDevice);
}
public void Dispose()
{
if (!_spriteBatch.IsDisposed)
{
_spriteBatch.Dispose();
_spriteBatch = null;
}
}
}
<强>更新强>
正确处理大量XNA对象非常重要(否则会导致内存泄漏)。请参阅上面的Foo.Dispose()和Game1.Dispose()覆盖。