如何使 XNA 应用程序重新加载自身
本文关键字:加载 新加载 何使 XNA 应用程序 | 更新日期: 2024-10-25 03:53:28
我试图在按下按钮 F 的那一刻让游戏全屏运行,但它不起作用,我在想,因为它必须首先重新加载应用程序才能生效,那么我该怎么做?
法典:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (keyboard.IsKeyDown(Keys.F))
{
WINDOW_WIDTH = 1280;
WINDOW_HEIGHT = 720;
}
base.Update(gameTime);
}
我做了一些简单的研究(谷歌搜索了"XNA 4 切换全屏"),发现有一个 ToggleFullScreen 方法:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.togglefullscreen.aspx
我还应用了我之前在评论中提到的修复程序,以避免每帧切换全屏
public KeyboardState PreviousKeyboardState = Keyboard.GetState();
public Boolean FullscreenMode = false;
public Vector2 WindowedResolution = new Vector2(800,600);
public Vector2 FullscreenResolution = new Vector2(1280, 720);
public void UpdateDisplayMode(bool fullscreen, Vector2 resolution)
{
graphics.PreferredBackBufferWidth = (int)resolution.X;
graphics.PreferredBackBufferHeight = (int)resolution.Y;
graphics.IsFullScreen = fullscreen;
graphics.ApplyChanges();
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
UpdateDisplayMode(FullscreenMode);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
var keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.F) && PreviousKeyboardState.IsKeyUp(Keys.F))
{
if (FullscreenMode)
UpdateDisplayMode(false, WindowedResolution);
else
UpdateDisplayMode(true, FullscreenResolution);
}
PreviousKeyboardState = keyboardState;
base.Update(gameTime);
}
它不更新的原因是,当您按"F"键时,您只需将变量设置为全屏大小。为了实际调整窗口大小,您必须执行与构造函数中相同的操作:
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
除此之外,您可能还需要设置graphics.IsFullScreen = true;