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 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078392
XNA游戏:重力感应相关推荐
- android 传感器小游戏,重力感应球游戏
重力感应球游戏是一款休闲益智类闯关游戏,有采用卡通的画风设计,超有趣的闯关玩法模式,在这里各种不同的游戏关卡等你来挑战,带给玩家最轻松有趣的闯关乐趣,非常的有意思,喜欢的小伙伴还在等什么,快来下载看看 ...
- android小游戏模版—重力感应
好久没更新博客了,今天来谈谈android小游戏---重力感应,一般在游戏里运用的比較多,比方这类游戏有:神庙逃亡.极品飞车,平衡球.三围重力迷宫,重力赛车等. 首先什么是重力感应.重力感应是指如今手 ...
- Unity3D 游戏引擎之平面小球重力感应详解【转】
http://blog.csdn.net/xys289187120/article/details/6969333 手机重力感应应该对大多数开发者并不陌生,在新一代智能手机Android ...
- 重力感应游戏可行性办法研究_完结
重力感应游戏可行性办法研究 一.手机中的重力感应组件 很多人都知道手机有重力感应这个功能,但是重力感应具体是什么,却并不是很了解.重力感应组件不是摄像头,人们知道有多少万像素,不是触摸屏,人们了解是电 ...
- 我的雷电游戏(重力感应控制)
算是第一个写的比较满意的游戏吧,肯定还有很多很多不足,希望高手能指点下,呵.现在在看图灵出版的一本android游戏的书籍,有点难,涉及底层有点多,都是JNI.NDK.linux shell脚本等,主 ...
- 基于野火M3开发板(STM32F103VET6)的迷宫小球(重力感应控制)游戏开发
2013-03-03 这是研一上学期<实时嵌入式系统实验>课程的大作业,是利用野火板的资源,加上一个AHRS(Attitude and Heading Reference System,姿 ...
- XNA游戏:各种输入测试 中
GestureDefinition.cs using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Inpu ...
- 重力感应机制和手机的屏幕绘画
2019独角兽企业重金招聘Python工程师标准>>> 摘要: 利用手机的重力感应机制和手机的屏幕绘画功能,实现一款躲避休闲游戏. 实现平台,工具: 采用java ADT工具,jav ...
- Android重力感应示例
android中的很多游戏的游戏都使用了重力感应的技术,就研究了一下重力感应 以屏幕的左下方为原点,箭头指向的方向为正.从-10到10,以浮点数为等级单位,想象以下情形: 手机屏幕向上(z轴朝天)水平 ...
最新文章
- ubuntu 18.04 添加快快捷方式
- 计算机虚拟化技术论文,【计算机网络论文】虚拟技术计算机网络论文(共1775字)...
- 常用 IO 模型图解介绍
- [ZJOI2007]报表统计(链表法+set)
- ic 卡获取帐号apdu指令_《全球行动》携手京东校园送福利 1000元京东卡等你拿
- Socket基础知识分享
- 20191112每日一句
- oracle 同时更新多表,在Oracle数据库中同时更新两张表的简单方法
- 大数据shipin教程_大数据自学全套视频教程下载
- java支付管理有源码_一款java版可接入微信、支付宝的三方支付系统项目
- php flash斗地主,flash斗地主
- 免费且好用的UML工具推荐
- 【杂七杂八】虚拟机win中 腾讯会议视频黑屏
- Elasticsearch:Elasticsearch 开发入门 - Golang
- Win10桌面图标箭头去掉的方法总结
- 人类面部表情数据集(12万张表情照片)
- 单模光纤与多模光纤的实际使用
- c盘java文件误删_清理C盘误删的文件怎么恢复
- 【spider】关于scrapy的安装的几个问题
- AOE图,最早活动时间最晚活动时间,关键路径关键节点