




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 / / This is the main type for your game / 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); / / 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. / protected override void Initialize() / TODO: Add your initialization logic here /初始化重力加速器 accelerometer = new Accelerometer(); /读取重力改变事件 accelerometer.ReadingChanged += new EventHandler(AccelerometerReadingChanged); /开始其中重力加速器 accelerometer.Start(); base.Initialize(); / / LoadContent will be called once per game and is the place to load / all of your content. / 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(readings); / / UnloadContent will be called once per game and is the place to unload / all content. / protected override void UnloadContent() / TODO: Unload any non ContentManager content here accelerometer.Stop(); / / Allows the game to run logic such as updating the world, / checking for collisions, gathering input, and playing audio. / / Provides a snapshot of timing values. 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); / / This is called when the game should draw itself. / / Provides a snapshot of timing values. 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 Acceleromet
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 29110-3-2:2018/AMD1:2025 EN Systems and software engineering - Lifecycle profiles for Very Small Entities (VSEs) - Part 3-2: Conformity certification scheme - Amendm
- 课件模板人物高清
- 综合材料手工培训大纲
- 高考成语教学课件
- 保险新人培训会
- 沃尔玛防损培训
- 广东会计制度自考试题及答案
- 广东国贸自考试题及答案
- 兰州法院考试题及答案
- 课二考试题及答案
- 人音版七年级音乐上册教案全册
- ECE-R90-欧盟第3版-中文版(R090r3e-01)
- 2023学年武汉市武昌区九年级语文上学期期中检测试卷附答案
- 渠道衬砌施工方案(渠道预制混凝土块)
- 不交社保劳动合同模板
- 2024年云南省中考数学试题(含答案)
- GB 14102.1-2024防火卷帘第1部分:通用技术条件
- 越野跑策划方案
- 《光学含沙量测量仪率定规范》
- 产值计算方案
- 冬季抢工措施方案
评论
0/150
提交评论