2014-04-14 88 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; 
    } 
相关问题