gpt4 book ai didi

windows-phone-8 - 如何使用 SupportedOrientations ="Portrait"获取手机的方向

转载 作者:行者123 更新时间:2023-12-04 16:17:02 26 4
gpt4 key购买 nike

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

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

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

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

enter image description here enter image description here

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

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 在横向模式下更改其坐标轴。

最佳答案

感谢您的大量回复:)

我发明的解决方案是固定页面方向:

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);
...

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

关于windows-phone-8 - 如何使用 SupportedOrientations ="Portrait"获取手机的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com