学习笔记30(手把手教用XNA开发WINPHONE7游戏4)
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)
C#
usingMicrosoft.Xna.Framework.Input;
usingMicrosoft.Xna.Framework.Input.Touch;
usingMicrosoft.Devices.Sensors;
4.添加附加的类变量用以保存触摸和加速度计状态:
(Code Snippet –Game Development with XNA– Gameplay Screen – more class variables)
C#
//Input Members
AccelerometerReadingEventArgsaccelState;
TouchCollectiontouchState;
AccelerometerAccelerometer;
5.在构造函数添加如下代码,初始化重力感应:
(Code Snippet –Game Development with XNA– Gameplay Screen – Accelerometer Initialization)
C#
Accelerometer =newAccelerometer();
if(Accelerometer.State ==SensorState.Ready)
{
Accelerometer.ReadingChanged += (s, e) =>
{
accelState = e;
};
Accelerometer.Start();
}
6.创建GameplayScreen类中的"输入"区域:
C#
#regionInput
#endregion
7.在“Input”区域给基类的HandleInput方法添加一个override,此方法将读取当前用户的输入数据,并在之后根据游戏中变化作出响应。
Note:在仿真设备中,鼠标点击将被认为是触摸或者键盘输入。但是使用真机的时候,键盘输入是不会发生的。
(Code Snippet –Game Development with XNA– Gameplan Screen – HandleInput method)
C#
/// <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)
C#
privatevoid finishCurrentGame()
{
foreach(GameScreenscreeninScreenManager.GetScreens())
screen.ExitScreen();
ScreenManager.AddScreen(newBackgroundScreen());
ScreenManager.AddScreen(newMainMenuScreen());
}
9.编译应用程序。
在此部分的过程中,您创建一个游戏的输入处理子系统。它将在创建游戏逻辑的下一个任务中使用。
下一篇就是最后一篇了,你的游戏就要出现了:)