2014-04-14 91 views
1

我將我的太空射擊遊戲從Windows Phone移植到Windows Store應用程序。在WP中,它總是以全肖像方向播放。SwapChainBackgroundPanel letterboxing Monogame Windows Store應用程序

對於Windows應用商店應用,儘管在橫向模式下,我想用左右兩邊的信箱來集中游戲屏幕。問題是我無法調整SwapChainBackgroundPanel的保證金屬性,所以遊戲始終與左側對齊,黑色屏幕位於右側。

這裏是我的代碼

public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     GamePage.Current.SizeChanged += OnWindowSizeChanged; 
     Content.RootDirectory = "Content"; 
    } 

    private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) 
    { 
     var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value; 
     double width = e.NewSize.Width; 
     double height = e.NewSize.Height; 

     // using Windows.Graphics.Display; 
     ResolutionScale resolutionScale = DisplayProperties.ResolutionScale; 
     string orientation = null; 

     if (ApplicationView.Value == ApplicationViewState.FullScreenLandscape) 
     { 
      orientation = "FullScreenLandscape"; 
      //Does not work because it's start on the center of the screen 
      //Black screen is on the left and place the game screen on the right 
      GamePage.Current.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center; 

      //Gives error - WinRT information: Setting 'Margin' property is 
      //not supported on SwapChainBackgroundPanel. 
      GamePage.Current.Margin = new Thickness(centerMargin, 0, 0, 0); 
     } 

     else if (ApplicationView.Value == ApplicationViewState.FullScreenPortrait) 
     { 
      orientation = "FullScreenPortrait"; 
     } 
     else if (ApplicationView.Value == ApplicationViewState.Filled) 
     { 
      orientation = "Filled"; 
     } 
     else if (ApplicationView.Value == ApplicationViewState.Snapped) 
     { 
      orientation = "Snapped"; 
     } 

     Debug.WriteLine("{0} x {1}. Scale: {2}. Orientation: {3}", 
      width.ToString(), height.ToString(), resolutionScale.ToString(), 
      orientation); 
    } 

的GamePage.xaml是默認

<SwapChainBackgroundPanel 
    x:Class="SpaceShooterXW8.GamePage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:SpaceShooterXW8" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d">  
</SwapChainBackgroundPanel> 
+1

正如一個方面說明,當你移植到考慮。 「SwapChainBackgroundPanel」在8.1和之後將不可用或更改,並且它們告訴我們切換到[SwapChainPanel](http://msdn.microsoft.com/en-us/library/windows/apps/windows。改爲ui.xaml.controls.swapchainpanel)。 –

回答

0

經過一番研究,我想我已經想通了感謝this blog post。對於那些處於類似情況的人,這是我所做的。

解決方案的優點在於信箱由Resolution類自動管理。我所要做的就是更新我的代碼東西batch.begin()線,如

 batch.Begin(SpriteSortMode.Deferred, 
      null, SamplerState.LinearClamp, 
      null, 
      null, 
      null, 
      Resolution.getTransformationMatrix()); 

要處理的分辨率變化的方向改變了我在Game1.cs

public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     GamePage.Current.SizeChanged += OnWindowSizeChanged; 
     Content.RootDirectory = "Content"; 
     Resolution.Init(ref graphics); 
     Resolution.SetVirtualResolution(480, 800); 
    } 


private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) 
    { 
     var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value; 
     App.AppWidth = (int)e.NewSize.Width; 
     App.AppHeight = (int)e.NewSize.Height; 
     Resolution.SetResolution(App.AppWidth, App.AppHeight, true); 
    } 

App.AppWidth的初始值利用這一點, App.AppHeightGamePage.xaml.cs中設置。

public GamePage(string launchArguments) 
    { 
     this.InitializeComponent(); 
     App.AppWidth = (int)Window.Current.Bounds.Width; 
     App.AppHeight = (int)Window.Current.Bounds.Height; 
     Current = this; 
     // Create the game. 
     _game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this); 

    } 

兩者都是App.xaml.cs

public static int AppWidth { get; set; } 
    public static int AppHeight { get; set; } 

到目前爲止,我遇到的唯一問題創造了全球靜態屬性,鼠標輸入不能擴展到屏幕分辨率的變化。我沒有觸摸屏來測試不幸的,但我認爲觸摸輸入應該縮放。如果有人測試觸摸,請分享您的發現。謝謝。

更新

我已經成功使用縮放鼠標輸入以下

public static Vector2 ScaleGesture(Vector2 position) 
    { 
     int x = (int)(position.X/(float)App.AppWidth * (float)Screen.ScreenWidth); 
     int y = (int)(position.Y/(float)App.AppHeight * (float)Screen.ScreenHeight); 
     var scaledPosition = new Vector2(x, y); 
     return scaledPosition; 
    } 
相關問題