Windows Phone 7之XNA游戏:重力感应
Windows Phone XNA游戏提供的重力传感器可以利用量测重力的原理判手机移动的方向,允许使用者利用摇动或甩动手机的方式控制游戏的执行,其原理和汽车的安全气囊相同,在侦测到汽车快速减速的时候立刻充气以保护驾驶人与乘客不会受伤。要使用重力传感器当做游戏程序的输入,以 XNA 为基础的游戏程序可以利用 Accelerometer 类别提供的功能启用/停用重力加速器,取得重力加速器的状态,以及处理重力加速器引发的事件。
Accelerometer 类别常用的属性
属性名称 |
说明 |
State |
管理重力加速器状态的属性,其型态为 SensorState 列举型态。有关 SensorState 列举型态合法的内容值可以参考表4 的说明。 |
Accelerometer 类别常用的方法
方法名称 |
说明 |
Start |
开始从重力加速器读取数据。 |
Stop |
结束从重力加速器读取数据。 |
Accelerometer 类别常用的事件
事件名称 |
说明 |
ReadingChanged |
当重力加速器读取到数据时会引发的事件。 |
处理 ReadingChanged 事件的事件处理程序的第二个参数的型态为 AccelerometerReadingEventArgs 类别,其 X、Y、与 X 属性的内容值代表智能型手机在 X 轴、Y 轴、和 Z 轴的加速方向,而不是三度空间的坐标,其单位为重力单位,也就是 G 力 (1G = 9.81 m/s2)。除了 X、Y、与 Z 三个属性以外,还有一个名称为 Timestamp 的属性,负责记录重力加速器读取数据的时间点。
请注意当手机放在平坦的桌面上,而且正面朝上的时候,AccelerometerReadingEventArgs 类别的 Z 字段的内容值会是 -1.0,表示 Z 轴承受 -1G 的重力,而当手机放在平坦的桌面上,而且正面朝下的时候,AccelerometerReadingEventArgs 类别的 Z 字段的内容值就会是 +1.0,表示 Z 轴承受 1G 的重力。
[说明]
透过 Accelerometer 类别的 State 属性取得的重力加速器状态是 SensorState 列举型态的数据,其合法的内容值请参考表 的说明:
内容值名称 |
说明 |
NotSupported |
未支持重力加速器。 |
Ready |
重力加速器处于可以处理数据的状态。 |
Initializing |
重力加速器正在初始化。 |
NoData |
未支持重力加速器。 |
NoPermissions |
呼叫者没有权限取用重力加速器接收到的数据。 |
Disabled |
重力加速器处于禁用的状态。 |
要使用重力加速器判断智能型手机加速的方向,首先您必须使用鼠标的右键点中 [Solution Explorer] 窗口中的项目名称,从出现的菜单选择 [Add Reference] 功能,然后于出现的窗口中选择名称为 Microsoft.Devices.Sensors 的组件,添加引用上去。
下面看一个例子:
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);
//开始其中重力加速器
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)
{
//触发UI更新
Deployment.Current.Dispatcher.BeginInvoke(() => NewReading(e));
}
//赋值XYZ的值
void NewReading(AccelerometerReadingEventArgs e)
{
X = e.X;
Y = e.Y;
Z = e.Z;
}
}
}
本文来自linzheng的博客,原文地址:http://www.cnblogs.com/linzheng/archive/2012/04/15/2450218.html
Windows Phone 7之XNA游戏:重力感应的更多相关文章
- android小游戏模版—重力感应
好久没更新博客了,今天来谈谈android小游戏---重力感应,一般在游戏里运用的比較多,比方这类游戏有:神庙逃亡.极品飞车,平衡球.三围重力迷宫,重力赛车等. 首先什么是重力感 ...
- 基于野火M3开发板(STM32F103VET6)的迷宫小球(重力感应控制)游戏开发
2013-03-03 这是研一上学期<实时嵌入式系统实验>课程的大作业,是利用野火板的资源,加上一个AHRS(Attitude and Heading Reference System,姿 ...
- 【Android开发学习笔记】【第九课】重力感应
概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...
- 移动终端学习2:触屏原生js事件及重力感应
如今智能移动设备已经渗透到人们生活的方方面面,用户数量也在不断迅速增长(市场研究机构 eMarketer 在今年初发表的趋势报告中预测,2014年至2018年,中国智能手机用户从总人口的 38.3%增 ...
- 移动端html5重力感应
下面是测试案例,只测试过itouch,iphone http://06wjin.sinaapp.com/billd/ http://06wjin.sinaapp.com/billd/test. ...
- H5之重力感应篇
手机的重力感应支持里,有两个主要的事件: 1. OrientationChange (在屏幕发生翻转的时候触发) 2. DeviceOrientation+DeviceMotion(重力感应与陀螺仪) ...
- coocs2d-html5在使用cocoseditor时调用设备的accelerometer来使用重力感应
在使用大牛touchsnow开发的插件cocoseditor开发游戏时遇到了一些问题,然后就试着解决.近期想试下coocs2d-html5是否能使用重力感应,发现是能够的.只是这个仅仅能在移动真机上測 ...
- Unity3D学习笔记——Android重力感应控制小球
一:准备资源 两张贴图:地图和小球贴图. 二:导入资源 在Assets下建立resources文件夹,然后将贴图导入. 三:建立场景游戏对象 1.建立灯光: 2.创建一个相机,配置默认. 3.建立一个 ...
- COCOS学习笔记--重力感应Acceleration
Cocos2dx重力感应Acceleration,准确来说叫加速度计,加速度计能够感应设备上X.Y.Z轴方向上线性加速度的变化.事实上叫"重力感应"或"重力加速度计&qu ...
随机推荐
- C#开发实例 鼠标篇
鼠标的操作控制: 鼠标是计算机的一个重要组成部分,有很多默认的设置,如双击时间间隔,闪烁频率,移动速度等,本篇使用C#获取这些基本的信息. 1.1获取鼠标信息 ①实例001 获取鼠标双击时间间隔 主要 ...
- HTML前端
1.<html>内容</html> 解释:HTML文档的文档标记,也成为HTML开始标记 功能:这对标记分别位于网页的最前端和最后端 <html>在最前段表示网页的 ...
- dubbo作为消费者注册过程分析
请支持原创: http://www.cnblogs.com/donlianli/p/3847676.html 作者当前分析的版本为2.5.x.作者在分析的时候,都是带着疑问去查看代码,debug进 ...
- js 实现图片预加载 (js操作 Image对象属性complete ,事件onload 异步加载图片)
通过js操纵DOM很多情况下都是为了实现和当前页html元素的异步载入,我谈谈对Image对象的一些认识.看个例子:<input type="button" name=&qu ...
- (转)SQL Server 中的事务和锁(三)-Range S-U,X-X 以及死锁
在上一篇中忘记了一个细节.Range T-K 到底代表了什么?Range T-K Lock 代表了在 SERIALIZABLE 隔离级别中,为了保护范围内的数据不被并发的事务影响而使用的一类锁模式(避 ...
- apple配置WIFI热点
打开AirPort打开设置偏好-共享,找到WIFI相关
- Java编程设计2
一般我们会以这种设计方式生产对象实例,如: 创建一个接口: public interface TestOpen { String getVirtualHost(); String getCapabil ...
- 把notepad++设置为系统全局文本默认打开应用
notepad++: 设置-->首选项-->文件关联->customize: 可以设置的常见默认打开文件后缀格式有:.log,.txt,.ini,.h,.c,.cpp,.java,. ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)
Problem A: The 3n + 1 problem Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 6[Submit][St ...
- Java Hour 32 Weather ( 5 ) struts2 – Action class
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 32 Struts2 Action 1 将action 映射到 ac ...