The gravity sensor provided by Windows Phone xNa allows users to determine the direction of mobile phone movement based on the principle of gravity measurement, allowing users to control game execution by shaking or shaking mobile phones, it works in the same way as the airbag of an automobile. It is immediately inflated when a vehicle is quickly slowed down to protect drivers and passengers from injury. Use Gravity Sensors as a gameProgramXNa-based game programs can use the features provided by the accelerometer category to enable/disable gravity accelerators, get the status of gravity accelerators, and handle events triggered by gravity accelerators.
Common attributes of the accelerometer category
Attribute name |
Description |
State |
Manages the properties of the gravity accelerator status. Its type is the sensorstate Enumeration type. For more information about valid content values of the sensorstate enumerated type, see table 4. |
Common Methods of accelerometer category
Method Name |
Description |
Start |
Start reading data from the gravity accelerator. |
Stop |
End reading data from the gravity accelerator. |
Common events of the accelerometer category
Event name |
Description |
Readingchanged |
Events that occur when the gravity accelerator reads data. |
The second parameter type of the event handler that processes the readingchanged event is the accelerometerreadingeventargs category, the X, Y, and X attributes of the content value represent the acceleration direction of the smart phone in the X axis, Y axis, and Z axis, rather than the coordinates of the Three-degree space. The unit is the gravity unit, that is, G force (1g = 9.81 m/s2 ). In addition to the X, Y, and Z attributes, there is also an attribute named timestamp, which records the time point at which the gravity accelerator reads data.
Note that when the mobile phone is placed on a flat desktop and the front is facing up, the content value of the z field in the accelerometerreadingeventargs category is-1.0, indicating that the Z bearing is subject to the gravity of-1g, when the mobile phone is placed on a flat desktop and face down, the content value of the z field in the accelerometerreadingeventargs category is + 1.0, indicating that the Z bearing is subject to the gravity of 1g.
[Description]
The gravity accelerator State obtained through the state attribute of the accelerometer category is the data of the sensorstate enumerated type. For valid content values, see the description in the table:
Content Value Name |
Description |
Notsupported |
Gravity accelerators are not supported. |
Ready |
The gravity accelerator is in a state where data can be processed. |
Initializing |
The gravity accelerator is being initialized. |
Nodata |
Gravity accelerators are not supported. |
Nopermissions |
The caller is not authorized to access the data received by the gravity accelerator. |
Disabled |
The gravity accelerator is disabled. |
To use a gravity accelerator to determine the direction of smart phone acceleration, you must first right-click the project name in the [Solution Explorer] window and select the [add reference] function from the menu that appears, in the displayed window, select Microsoft. devices. sensors components, add references.
The following is an example:
Using System; Using System. windows; Using System. Collections. Generic; Using System. LINQ; Using Microsoft. xNa. Framework; Using Microsoft. xNa. Framework. Audio; Using Microsoft. xNa. Framework. content; Using Microsoft. xNa. Framework. gamerservices; Using Microsoft. xNa. Framework. graphics; Using Microsoft. xNa. Framework. input; Using Microsoft. xNa. Framework. Input. Touch; Using Microsoft. xNa. Framework. Media; Using Microsoft. devices. sensors; Namespace Accelerometersample { /// <Summary> /// This is the main type for your game /// </Summary> Public Class Game1: Microsoft. xNa. Framework. Game {graphicsdevicemanager graphics; spritebatch; spritefont readingsfont; // Font Resources Accelerometer accelerometer; // Gravity accelerator Double X; Double Y; Double Z; Public Game1 () {graphics = New Graphicsdevicemanager ( This ); Content. rootdirectory = " Content " ; // Frame rate is 30 FPS by default for Windows Phone. Targetelapsedtime = timespan. fromticks ( 333333 );} /// <Summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// Related content. Calling base. initialize will enumerate through any components /// And initialize them as well. /// </Summary> Protected Override Void Initialize (){ // Todo: add your initialization logic here // Initialize gravity accelerator Accelerometer =New Accelerometer (); // Read gravity change events Accelerometer. readingchanged + = New Eventhandler <accelerometerreadingeventargs> (Accelerometerreadingchanged ); // Start with gravity accelerator Accelerometer. Start (); Base . Initialize ();} /// <Summary> /// Loadcontent will be called once per game and is the place to load /// All of your content. /// </Summary> Protected Override Void Loadcontent (){ // Create a new spritebatch, which can be used to draw textures. Spritebatch = New Spritebatch (graphicsdevice ); // Todo: Use this. content to load your game content here // Load font Resources Readingsfont = content. Load <spritefont> ( " Readings " );} /// <Summary> /// Unloadcontent will be called once per game and is the place to unload /// All content. /// </Summary> Protected Override Void Unloadcontent (){ // Todo: unload any non contentmanager content here Accelerometer. Stop ();} /// <Summary> /// Allows the game to run logic such as updating the world, /// Checking for collisions, gathering input, and playing audio. /// </Summary> /// <Param name = "gametime"> Provides a snapshot of timing values. </Param> Protected Override Void Update (gametime ){ // Allows the game to exit If (Gamepad. getstate (playerindex. One). Buttons. Back = Buttonstate. Pressed) This . Exit (); // Todo: add your update logic here Base . Update (gametime );} /// <Summary> /// This is called when the game shoshould draw itself. /// </Summary> /// <Param name = "gametime"> Provides a snapshot of timing values. </Param> Protected Override Void Draw (gametime) {graphicsdevice. Clear (color. cornflowerblue ); // Todo: add your drawing code here Spritebatch. Begin (); // Draw text Spritebatch. drawstring (readingsfont, " X: " + X. tostring ( " 0.00 " ), New Vector2 ( 50 , 50 ), Color. White); spritebatch. drawstring (readingsfont, " Y: " + Y. tostring ( " 0.00 " ), New Vector2 ( 50 , 75 ), Color. White); spritebatch. drawstring (readingsfont, " Z: " + Z. tostring ( " 0.00 " ),New Vector2 ( 50 , 100 ), Color. White); spritebatch. End (); Base . Draw (gametime );} Void Accelerometerreadingchanged ( Object Sender, accelerometerreadingeventargs e ){ // Trigger UI update Deployment. Current. Dispatcher. begininvoke () => Newreading (e ));} // Returns the value of XYZ. Void Newreading (accelerometerreadingeventargs e) {x = E. X; y = E. Y; Z = E. Z ;}}}