views:

421

answers:

2

Similar to this question, but looking for an answer that will work in the context of an XNA game.

How can I determine whether the device is in a landscape or portrait orientation? The answer given in the general question relies upon functionality built into PhoneApplicationPage. AFAIK, you wouldn't normally be using that class within the context of an XNA game on Windows Phone 7.

+3  A: 

From Nick Gravelyn: http://forums.xna.com/forums/p/49684/298915.aspx#298915 Accelerometer isn't in the XNA Framework anymore. You can access it through these steps:

1) Add a reference to Microsoft.Devices.Sensors.dll 2) Add 'using Microsoft.Devices.Sensors;' to your using statements. 3) Hook up an event and start reading the accelerometer:

try
{
AccelerometerSensor.Default.ReadingChanged += Default_ReadingChanged;
AccelerometerSensor.Default.Start();
}
catch (AccelerometerStartFailedException)
{
}

4) Add the event handler itself:

void Default_ReadingChanged(object sender, AccelerometerReadingAsyncEventArgs e)
{
}

And you're good to go. Keep in mind, though, that accelerometer doesn't work with the emulator so there's no way to really test this without a device. You do need that try/catch because Start will throw an exception in the emulator because it doesn't support accelerometer.

mjf
Given the number of accelerometer-based iPhone games, removing support from XNA seems like an odd choice.
Joel Mueller
It's because they needed to expose it for silverlight apps as well, so they couldn't have it be dependent on xna apps
Joel Martinez
A: 

Here's a post from Shawn Hargreaves' Blog

http://blogs.msdn.com/b/shawnhar/archive/2010/07/12/orientation-and-rotation-on-windows-phone.aspx?utm_source=twitterfeed&utm_medium=twitter

If you want to automatically switch between both landscape and portrait orientations as the phone is rotated:

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

Switching between LandscapeLeft and LandscapeRight can be handled automatically with no special help from the game, and is therefore enabled by default. But switching between landscape and portrait alters the backbuffer dimensions (short-and-wide vs. tall-and-thin), which will most likely require you to adjust your screen layout. Not all games will be able to handle this (and some designs only make sense one way up), so dynamic switching between landscape and portrait is only enabled for games that explicitly opt-in by setting SupportedOrientations.

Christopher Scott