2013-12-18 55 views
1

我試圖讓遊戲全屏按下F按鈕,但它不起作用,我在想,因爲它必須先爲它重新加載應用程序採取影響,那麼我該怎麼做?如何使XNA應用程序自行重新加載

代碼:

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); 
} 
+1

每秒做60次可能不是那麼明智。考慮將鍵盤狀態存儲在更新結束時的屬性(PreviousKeyboardState)中(僅在base.update之前,但在所有其他邏輯之後),並確保用戶沒有通過檢查PreviousKeyboardState.IsKeyUp(Keys鍵)來按住f鍵。 F) –

+0

我該怎麼做?我用鼠標點擊了同樣的東西,但是如何通過點擊按鈕來完成呢? – YayCoding

回答

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); 
} 
+0

謝謝,它的工作!但是,我也希望它改變屏幕的分辨率,如果我將其保留在800 x 600,全屏屏幕將會是800 x 600.我嘗試更改if語句中的WINDOW_WIDTH/HEIGHT,但它沒有工作,任何想法? – YayCoding

+0

編輯答案以結合更改分辨率。我對這些值進行了硬編碼,因此您可能需要使用WINDOW_HEIGHT/WIDTH或其他內容。 –

+0

它的工作,嗚呼!但是,如果我想切換回來?我試圖做到這一點,但它只是切換全屏,並不會將分辨率更改回小於屏幕尺寸的窗口。 – YayCoding

0

它之所以沒有更新是因爲當你按下「F」鍵,你只需設置你的變量爲全屏大小。 爲了真正調整你必須做的一樣在構造函數的窗口:

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

此外,你可能還需要設置graphics.IsFullScreen = true;

+0

仍然不能正常工作,我試着把'this.'放在graphics.PreferredBackBufferWidth/graphics.PreferredBackBufferWidth前面,但它也沒有工作。 – YayCoding

+0

@ user3074243嘗試添加'graphics.ApplyChanges();'後設置寬度和高度 – HellGate

相關問題