【问题标题】:How to get orientation of the phone with SupportedOrientations="Portrait"如何使用 SupportedOrientations="Portrait" 获取手机的方向
【发布时间】:2014-01-27 07:53:46
【问题描述】:

我使用 MonoGame 库编写了一个简单的游戏。据我所知,MonoGame(与 XNA 不同)不会自动支持更改手机的方向,因此有必要使用 RenderTarget2D 然后以正确的方向绘制它。为此,我需要检测手机的当前方向。

为了获得 OrientationChanged 事件,我必须允许 GamePage.xaml 使用不同的方向:

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

问题在于,在这种情况下,我的 GamePage 开始自动更改方向,并且横向位置的精灵被水平拉伸。似乎在横向位置手机认为它沿水平方向的像素数与纵向方向相同(480 像素)。

此效果无需任何手动旋转即可发生。绘图代码为:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    spriteBatch.Draw(t, Vector2.Zero, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
    spriteBatch.End();

    base.Draw(gameTime);
}

其中一种解决方案是在 GamePage.xaml 中禁止不同的方向:

SupportedOrientations="Portrait" Orientation="Portrait"

但在这种情况下,我无法获得 OrientationChanged 事件(GamePage.OrientationChanged 或 Game.Window.OrientationChanged)。我将以下代码放在 Game 类的构造函数中,但没有帮助

graphics.SupportedOrientations = DisplayOrientation.Portrait |
                                 DisplayOrientation.PortraitDown |
                                 DisplayOrientation.LandscapeLeft |
                                 DisplayOrientation.LandscapeRight;

graphics.ApplyChanges();
this.Window.OrientationChanged += OrientationChanged;

请告诉我如何获取 OrientationChanged 事件,同时不允许 GamePage 在横向模式下更改其坐标轴。

【问题讨论】:

    标签: windows-phone-8 monogame


    【解决方案1】:

    感谢您的大量回复:)

    我被发明了的解决方案是修复页面方向:

    SupportedOrientations="Portrait" Orientation="Portrait"
    

    并使用加速度计来获取手机的实际位置:

    public class Game1 : Game
    {
        Accelerometer accelSensor;
        PageOrientation currentOrientation = PageOrientation.None;
        PageOrientation desiredOrientation = PageOrientation.None;
        ...
    
        public Game1()
        {
            accelSensor = new Accelerometer();
            accelSensor.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs> AccelerometerReadingChanged);
            ...
    
        private void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            double angle = Math.Atan2(-e.X, e.Y) * 180.0 / Math.PI;
            double delta = 15;
    
            if (angle > -45 + delta && angle < 45 - delta) desiredOrientation = PageOrientation.PortraitDown;
            if (angle > 45 + delta && angle < 135 - delta) desiredOrientation = PageOrientation.LandscapeLeft;
            if (angle > -135 + delta && angle < -45 - delta) desiredOrientation = PageOrientation.LandscapeRight;
            if ((angle >= -180 && angle < -135 - delta) || (angle > 135 + delta && angle <= 180)) desiredOrientation = PageOrientation.PortraitUp;
        }
    
        protected override void Update(GameTime gameTime)
        {
            if (desiredOrientation != currentOrientation) SetOrientation(desiredOrientation);
            ...
    

    如果你知道更好的方法请告诉我...

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多