XNA Game Studio游戏输入
昨天的双11让人感触颇多,幸好有http://winphone.us/和博客园还有技术陪伴我,还有很多园子里的朋友,大家一定不要放弃自己的梦想。
在这个环节,你将为游戏添加输入部分。对于windows Phone手机,他的输入主要是通过触摸板或者重力感应。由于Windows Phone模拟器不提供重力感应,我们提供了一个通过键盘来模拟重力感应的解决方案。这个只供模拟设备测试用,不能在真机上使用。
1.添加Microsoft.Device.Sensors程序集的引用。
图1
Adding a reference to the Microsoft.Devices.Sensors assembly
Note:要添加引用,在解决方案资源管理器中的AlienGame项目下,右键点击References:
2.打开GameplayScreen.cs(如果没有打开的话).
3.添加如下命名申请:
(Code Snippet –Game Development with XNA– Gameplay Screen – more using statements)
usingMicrosoft.Xna.Framework.Input;
usingMicrosoft.Xna.Framework.Input.Touch;
usingMicrosoft.Devices.Sensors;
4.添加附加的类变量用以保存触摸和加速度计状态:
(Code Snippet –Game Development with XNA– Gameplay Screen – more class variables)
//Input Members
AccelerometerReadingEventArgsaccelState;
TouchCollectiontouchState;
AccelerometerAccelerometer;
5.在构造函数添加如下代码,初始化重力感应:
(Code Snippet –Game Development with XNA– Gameplay Screen – Accelerometer Initialization)
Accelerometer =newAccelerometer();
if(Accelerometer.State ==SensorState.Ready)
{
Accelerometer.ReadingChanged += (s, e) =>
{
accelState = e;
};
Accelerometer.Start();
}
6.创建GameplayScreen类中的"输入"区域:
7.在“Input”区域给基类的HandleInput方法添加一个override,此方法将读取当前用户的输入数据,并在之后根据游戏中变化作出响应。
Note:在仿真设备中,鼠标点击将被认为是触摸或者键盘输入。但是使用真机的时候,键盘输入是不会发生的。
(Code Snippet –Game Development with XNA– Gameplan Screen – HandleInput method)
/// <summary>
///Input helper method provided by GameScreen.Packages up the various input
///values for ease of use.Here it checks for pausing and handles controlling
///the player's tank.
/// </summary>
/// <param name="input">The state of the gamepads</param>
publicoverridevoidHandleInput(InputStateinput)
{
if(input ==null)
thrownewArgumentNullException("input");
if(input.PauseGame)
{
if(gameOver ==true)
finishCurrentGame();
}
else
{
touchState =TouchPanel.GetState();
boolbuttonTouched =false;
//interpret touch screen presses
foreach(TouchLocationlocationintouchState)
{
switch(location.State)
{
caseTouchLocationState.Pressed:
buttonTouched =true;
break;
caseTouchLocationState.Moved:
break;
caseTouchLocationState.Released:
break;
}
}
floatmovement = 0.0f;
if(accelState !=null)
{
if(Math.Abs(accelState.X) > 0.10f)
{
if(accelState.X > 0.0f)
movement = 1.0f;
else
movement = -1.0f;
}
}
//TODO: Update player Velocity over X axis #1
//This section handles tank movement.We only allow one "movement" action
//to occur at once so that touchpad devices don't get double hits.
KeyboardStatekeyState =Keyboard.GetState();
if(input.CurrentGamePadStates[0].DPad.Left == ButtonState.Pressed || keyState.IsKeyDown(Keys.Left))
{
//TODO: Update player velocity over X axis #2
}
elseif(input.CurrentGamePadStates[0].DPad.Right == ButtonState.Pressed || keyState.IsKeyDown(Keys.Right))
{
//TODO: Update player velocity over X axis #3
}
else
{
//TODO: Update player velocity over X axis #4
}
// B button, or pressing on the upper half of the pad or space on keyboard or touching the touch panel fires the weapon.
if(input.CurrentGamePadStates[0].IsButtonDown(Buttons.B) || input.CurrentGamePadStates[0].IsButtonDown(Buttons.A) || input.CurrentGamePadStates[0].ThumbSticks.Left.Y
> 0.25f ||
keyState.IsKeyDown(Keys.Space) || buttonTouched)
{
if(!gameOver)
{
//TODO: Fire the bullet
}
elseif(gameOver)
finishCurrentGame();
}
}
}
8.添加Helper方法来完成游戏:
(Code Snippet –Game Development with XNA– Gameplay Screen – finishCurrentGame method)
privatevoid finishCurrentGame()
{
foreach(GameScreenscreeninScreenManager.GetScreens())
screen.ExitScreen();
ScreenManager.AddScreen(newBackgroundScreen());
ScreenManager.AddScreen(newMainMenuScreen());
}
9.编译应用程序。
在此部分的过程中,您创建一个游戏的输入处理子系统。它将在创建游戏逻辑的下一个任务中使用。
下一篇就是最后一篇了,你的游戏就要出现了:)
分享到:
相关推荐
wp7 xna开发最强教程,让你最快的学习xna开发,能够写出一个小的游戏。
XNA平台游戏开发中文教程,对于入门来说是不可多得的靓文.
Microsoft 的 XNA 游戏开发 资料。
基于XNA框架开发的连连看小游戏,使用C#语言
xna游戏的开发介绍以及编程代码 是学习xna开发的必读之书
Direct 3D与XNA游戏开发源码,很优秀的书,很优秀的源代码。
《Windows Phone 7游戏高级编程:使用XNA Game Studio 4》包含了大量真实示例,对WindowsPhone 7游戏开发做了深入探索,是手边必备的一本全面教程。本书首先介绍了Windows Phone 7的特性,然后讲解了游戏开发过程会...
该游戏是基于xna4.0开发的小游戏,属于简单的AI游戏, 含源代码。
本游戏是使用C#语言结合XNA技术开发的基于VS2010平台的小游戏,游戏有完整的游戏界面,不同难度的游戏关卡,音效几分功能齐全,视觉效果良好,希望大家能够喜欢
XNA 游戏开发 C# 安装包
Direct 3D与XNA游戏开发光盘资源(6卷).part3
XNA游戏开发SDK安装包,安装后在Visual Studio中可以做XNA游戏项目
国内第一本XNA教程中文《XNA入门指南》,经《A Beginners Guild to XNA》原作者 Mike Fleischauer 书面授权中国XNA开发网翻译!书中如有错误请访问www.ChinaXNA.com 或者 www.XNADev.cn
Direct.3D和XNA游戏开发基础(C#语言版)
基于XNA开发的3D游戏源码,适合初学者!
本系统共享全部源代码,对XNA游戏开发有兴趣的读者或是专业人士均有一定的参考价值。
介绍了Direct_3D的基础知识与XNA游戏开发基础
用XNA开发的,类似雷电的射击小游戏。可以看一下,有游戏开发兴趣的人不能错过。
XNA学习资料,XNAXNA学习资料,XNAXNA学习资料,XNAXNA学习资料,XNA