XNA游戲開(kāi)發(fā)中重力感應(yīng)的使用
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è)例子:
- 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 spriteBatch;
- SpriteFont readingsFont;//字體資源
- Accelerometer accelerometer;//重力加速器
- 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
- //初始化重力加速器
- accelerometer = new Accelerometer();
- //讀取重力改變事件
- accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);
- //開(kāi)始其中重力加速器
- 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
- //加載字體資源
- 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 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 should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- //繪制文字
- 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)
- {
- //觸發(fā)UI更新
- Deployment.Current.Dispatcher.BeginInvoke(() => NewReading(e));
- }
- //賦值XYZ的值
- void NewReading(AccelerometerReadingEventArgs e)
- {
- X = e.X;
- Y = e.Y;
- Z = e.Z;
- }
- }
- }
原文鏈接:http://www.cnblogs.com/linzheng/archive/2012/04/15/2450218.html