Using the accelerometer
Amongst the Windows Phone 7 hardware sensors is the accelerometer. This sensor can be used to detect tilt in three dimensions. Some games, like maze games, are very suited for using the accelerometer as an input device for movement. XNA Game Studio doesn't support the accelerometer by default; instead we have to add a reference to Microsoft.Devices.Sensors
manually. We can do this by right-clicking our solution and choosing Add Reference. After we have added the reference, we can make a new instance of the Accelerometer
class (if the device supports the accelerometer). Note that you can have no more than ten instances of the Accelerometer
class. This class has support for starting and stopping the accelerometer, along with an event handler called CurrentValueChanged
. This event handler is called each time the accelerometer detects a new value.
The code is fairly straight forward:
if (Accelerometer.IsSupported) { // Max 10 simultanious instances var _accelerometer...