如何使XNA应用程序重新加载自己

时间:2013-12-18 11:10:08

标签: c# xna

我正在尝试让你按下按钮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);
}

2 个答案:

答案 0 :(得分:1)

我做了一些轻微的研究(用Google搜索'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);
}

答案 1 :(得分:0)

它没有更新的原因是因为当您按下'F'键时,您只需将变量设置为全屏大小。 为了实际调整窗口大小,你必须像在构造函数中一样:

graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;

除此之外,您可能还想设置graphics.IsFullScreen = true;