C# 如何使XNA应用程序重新加载自身
我试图让游戏在你按下F键的那一刻全屏运行,但它不起作用,我在想,因为它必须先重新加载应用程序才能生效,那么我该怎么做呢 代码:C# 如何使XNA应用程序重新加载自身,c#,xna,C#,Xna,我试图让游戏在你按下F键的那一刻全屏运行,但它不起作用,我在想,因为它必须先重新加载应用程序才能生效,那么我该怎么做呢 代码: 它不更新的原因是,当您按下“F”键时,您只需将变量设置为全屏大小。 为了实际调整窗口大小,您必须执行与构造函数相同的操作: graphics.PreferredBackBufferWidth = WINDOW_WIDTH; graphics.PreferredBackBufferHeight = WINDOW_HEIGHT; 除此之外,您可能还需要设置graphics
它不更新的原因是,当您按下“F”键时,您只需将变量设置为全屏大小。 为了实际调整窗口大小,您必须执行与构造函数相同的操作:
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
除此之外,您可能还需要设置
graphics.IsFullScreen=true代码>我做了一些光线研究(谷歌搜索“XNA 4切换全屏”),发现有一种切换全屏的方法:
我还应用了我前面在评论中提到的修复程序,以避免在每一帧中切换全屏
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);
}
每秒这样做60次可能不是那么明智。考虑在您的更新结束之前(在BaseUpdate之前,但在所有其他逻辑之后)将KeBOBOADSTATE存储在一个属性(PrimviouKeBoAtDead状态)中,并且确保用户没有通过检查前面的键盘状态来按住F键。IsKeyUp(KEY.F)我该怎么做?我用鼠标单击做了类似的事情,但如何用按钮单击来做呢?仍然不起作用,我尝试将这个。
放在图形前面。PreferredBackBufferWidth/graphics.PreferredBackBufferWidth,但也不起作用。@user3074243尝试添加graphics.ApplyChanges()代码>设置宽度和高度后,谢谢,成功了!但是,我也希望它改变屏幕的分辨率,如果我把它保持在800x600,全屏的分辨率将是800x600。我尝试在if语句中更改窗口的宽度/高度,但没有效果,有什么想法?编辑答案以合并分辨率更改。我硬编码了这些值,所以你可能会想用你的窗户的高度/宽度或者其他什么。它成功了,呜呜!但如果我想把它切换回去?我尝试过这样做,但它只是切换全屏,并且不会将分辨率更改回小于屏幕大小的窗口。我现在在我的帖子中添加了更新分辨率的功能。但这通常不是您想要做的事情,因为您可能需要不同分辨率的资产的多个版本。也;如果你能接受我的答案,那就太棒了:)我使用的代码和你说的一模一样,现在实际的程序不会打开,我尝试打开它,然后构建,进入调试模式,然后返回正常模式。
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);
}