自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

XNA游戲開(kāi)發(fā)中重力感應(yīng)的使用

移動(dòng)開(kāi)發(fā) 游戲開(kāi)發(fā)
Windows Phone XNA游戲提供的重力傳感器可以利用量測(cè)重力的原理判手機(jī)移動(dòng)的方向,允許使用者利用搖動(dòng)或甩動(dòng)手機(jī)的方式控制游戲的執(zhí)行。

Windows Phone XNA游戲提供的重力傳感器可以利用量測(cè)重力的原理判手機(jī)移動(dòng)的方向,允許使用者利用搖動(dòng)或甩動(dòng)手機(jī)的方式控制游戲的執(zhí)行,其原理和汽車的安全氣囊相同,在偵測(cè)到汽車快速減速的時(shí)候立刻充氣以保護(hù)駕駛?cè)伺c乘客不會(huì)受傷。要使用重力傳感器當(dāng)做游戲程序的輸入,以XNA為基礎(chǔ)的游戲程序可以利用Accelerometer類別提供的功能啟用/停用重力加速器,取得重力加速器的狀態(tài),以及處理重力加速器引發(fā)的事件。

Accelerometer類別常用的屬性

屬性名稱 說(shuō)明
State 管理重力加速器狀態(tài)的屬性,其型態(tài)為SensorState列舉型態(tài)。有關(guān)SensorState列舉型態(tài)合法的內(nèi)容值可以參考表4的說(shuō)明。

Accelerometer類別常用的方法

方法名稱 說(shuō)明
Start 開(kāi)始從重力加速器讀取數(shù)據(jù)。
Stop 結(jié)束從重力加速器讀取數(shù)據(jù)。

Accelerometer類別常用的事件

事件名稱 說(shuō)明
ReadingChanged 當(dāng)重力加速器讀取到數(shù)據(jù)時(shí)會(huì)引發(fā)的事件。

處理ReadingChanged事件的事件處理程序的第二個(gè)參數(shù)的型態(tài)為AccelerometerReadingEventArgs 類別,其 X、Y、與 X 屬性的內(nèi)容值代表智能型手機(jī)在 X 軸、Y 軸、和 Z 軸的加速方向,而不是三度空間的坐標(biāo),其單位為重力單位,也就是 G 力 (1G = 9.81 m/s2)。除了 X、Y、與 Z 三個(gè)屬性以外,還有一個(gè)名稱為 Timestamp 的屬性,負(fù)責(zé)記錄重力加速器讀取數(shù)據(jù)的時(shí)間點(diǎn)。

圖例

請(qǐng)注意當(dāng)手機(jī)放在平坦的桌面上,而且正面朝上的時(shí)候,AccelerometerReadingEventArgs類別的 Z 字段的內(nèi)容值會(huì)是 -1.0,表示 Z 軸承受 -1G 的重力,而當(dāng)手機(jī)放在平坦的桌面上,而且正面朝下的時(shí)候,AccelerometerReadingEventArgs 類別的Z字段的內(nèi)容值就會(huì)是 +1.0,表示 Z 軸承受 1G 的重力。

說(shuō)明

透過(guò)Accelerometer類別的State屬性取得的重力加速器狀態(tài)是SensorState列舉型態(tài)的數(shù)據(jù),其合法的內(nèi)容值請(qǐng)參考表的說(shuō)明:

內(nèi)容值名稱 說(shuō)明
NotSupported 未支持重力加速器。
Ready 重力加速器處于可以處理數(shù)據(jù)的狀態(tài)。
Initializing 重力加速器正在初始化。
NoData 未支持重力加速器。
NoPermissions 呼叫者沒(méi)有權(quán)限取用重力加速器接收到的數(shù)據(jù)。
Disabled 重力加速器處于禁用的狀態(tài)。

要使用重力加速器判斷智能型手機(jī)加速的方向,首先您必須使用鼠標(biāo)的右鍵點(diǎn)中Solution Explorer窗口中的項(xiàng)目名稱,從出現(xiàn)的菜單選擇Add Reference功能,然后于出現(xiàn)的窗口中選擇名稱為 Microsoft.Devices.Sensors的組件,添加引用上去。

下面看一個(gè)例子:

  1. using System;  
  2. using System.Windows;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using Microsoft.Xna.Framework;  
  6. using Microsoft.Xna.Framework.Audio;  
  7. using Microsoft.Xna.Framework.Content;  
  8. using Microsoft.Xna.Framework.GamerServices;  
  9. using Microsoft.Xna.Framework.Graphics;  
  10. using Microsoft.Xna.Framework.Input;  
  11. using Microsoft.Xna.Framework.Input.Touch;  
  12. using Microsoft.Xna.Framework.Media;  
  13.  
  14. using Microsoft.Devices.Sensors;  
  15.  
  16. namespace AccelerometerSample  
  17. {  
  18.     /// <summary> 
  19.     /// This is the main type for your game  
  20.     /// </summary> 
  21.     public class Game1 : Microsoft.Xna.Framework.Game  
  22.     {  
  23.         GraphicsDeviceManager graphics;  
  24.         SpriteBatch spriteBatch;  
  25.         SpriteFont readingsFont;//字體資源  
  26.         Accelerometer accelerometer;//重力加速器  
  27.         double X;  
  28.         double Y;  
  29.         double Z;  
  30.           
  31.         public Game1()  
  32.         {  
  33.             graphics = new GraphicsDeviceManager(this);  
  34.             Content.RootDirectory = "Content";  
  35.  
  36.             // Frame rate is 30 fps by default for Windows Phone.  
  37.             TargetElapsedTime = TimeSpan.FromTicks(333333);  
  38.  
  39.         }  
  40.  
  41.         /// <summary> 
  42.         /// Allows the game to perform any initialization it needs to before starting to run.  
  43.         /// This is where it can query for any required services and load any non-graphic  
  44.         /// related content.  Calling base.Initialize will enumerate through any components  
  45.         /// and initialize them as well.  
  46.         /// </summary> 
  47.         protected override void Initialize()  
  48.         {  
  49.             // TODO: Add your initialization logic here  
  50.             //初始化重力加速器  
  51.             accelerometer = new Accelerometer();  
  52.             //讀取重力改變事件  
  53.             accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);  
  54.             //開(kāi)始其中重力加速器  
  55.             accelerometer.Start();  
  56.  
  57.             base.Initialize();  
  58.         }  
  59.  
  60.         /// <summary> 
  61.         /// LoadContent will be called once per game and is the place to load  
  62.         /// all of your content.  
  63.         /// </summary> 
  64.         protected override void LoadContent()  
  65.         {  
  66.             // Create a new SpriteBatch, which can be used to draw textures.  
  67.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  68.  
  69.             // TODO: use this.Content to load your game content here  
  70.             //加載字體資源  
  71.             readingsFont = Content.Load<SpriteFont>("readings");  
  72.  
  73.         }  
  74.  
  75.         /// <summary> 
  76.         /// UnloadContent will be called once per game and is the place to unload  
  77.         /// all content.  
  78.         /// </summary> 
  79.         protected override void UnloadContent()  
  80.         {  
  81.             // TODO: Unload any non ContentManager content here  
  82.             accelerometer.Stop();  
  83.         }  
  84.  
  85.         /// <summary> 
  86.         /// Allows the game to run logic such as updating the world,  
  87.         /// checking for collisions, gathering input, and playing audio.  
  88.         /// </summary> 
  89.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  90.         protected override void Update(GameTime gameTime)  
  91.         {  
  92.             // Allows the game to exit  
  93.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
  94.                 this.Exit();  
  95.  
  96.             // TODO: Add your update logic here  
  97.  
  98.             base.Update(gameTime);  
  99.         }  
  100.  
  101.         /// <summary> 
  102.         /// This is called when the game should draw itself.  
  103.         /// </summary> 
  104.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  105.         protected override void Draw(GameTime gameTime)  
  106.         {  
  107.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  108.  
  109.             // TODO: Add your drawing code here  
  110.             spriteBatch.Begin();  
  111.             //繪制文字  
  112.             spriteBatch.DrawString(readingsFont, "X: " + X.ToString("0.00"), new Vector2(50, 50), Color.White);  
  113.             spriteBatch.DrawString(readingsFont, "Y: " + Y.ToString("0.00"), new Vector2(50, 75), Color.White);  
  114.             spriteBatch.DrawString(readingsFont, "Z: " + Z.ToString("0.00"), new Vector2(50, 100), Color.White);  
  115.             spriteBatch.End();  
  116.  
  117.             base.Draw(gameTime);  
  118.         }  
  119.  
  120.  
  121.         void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)  
  122.         {  
  123.             //觸發(fā)UI更新  
  124.             Deployment.Current.Dispatcher.BeginInvoke(() => NewReading(e));  
  125.         }  
  126.         //賦值XYZ的值  
  127.         void NewReading(AccelerometerReadingEventArgs e)  
  128.         {  
  129.             X = e.X;  
  130.             Y = e.Y;  
  131.             Z = e.Z;  
  132.         }  
  133.     }  

 

 

[[77012]]

原文鏈接:http://www.cnblogs.com/linzheng/archive/2012/04/15/2450218.html

責(zé)任編輯:王曉東 來(lái)源: 博客
相關(guān)推薦

2013-05-21 11:24:07

Android游戲開(kāi)發(fā)Sensor重力感應(yīng)

2012-05-22 14:26:15

XNA 橫豎屏設(shè)置

2010-01-27 17:17:42

Android重力感應(yīng)

2012-12-24 09:01:41

iOSUnity3D

2013-05-21 11:26:49

Android游戲開(kāi)發(fā)Sensor感應(yīng)

2010-09-08 11:26:26

Windows PhoXNA 4.0 3D游戲開(kāi)發(fā)

2010-08-10 09:11:12

Windows PhoNXA

2012-05-25 15:20:38

XNA

2013-05-21 11:20:37

Android游戲開(kāi)發(fā)View手勢(shì)識(shí)別

2011-08-19 11:03:37

iPhone應(yīng)用三軸感應(yīng)器

2013-05-20 16:12:23

2011-05-16 17:19:29

游戲開(kāi)發(fā)iPhone

2011-08-12 08:56:31

JavaScript

2010-03-08 19:03:23

Python腳本

2024-09-23 10:00:00

Python游戲開(kāi)發(fā)

2011-08-24 13:56:12

Lua游戲

2011-08-17 13:27:08

iPhone游戲開(kāi)發(fā)objective-c

2013-03-22 16:49:12

2013-06-27 13:46:41

游戲開(kāi)發(fā)

2011-06-13 18:21:12

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)