读书人

手把手教用XNA开发winphone7游戏(4)

发布时间: 2012-07-16 15:45:00 作者: rapoo

手把手教用XNA开发winphone7游戏(四)

图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#

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Input.Touch;

using Microsoft.Devices.Sensors;

4. 添加附加的类变量用以保存触摸和加速度计状态:

(Code Snippet Game Development with XNA Gameplay Screen more class variables)

C#

//Input Members

AccelerometerReadingEventArgs accelState;

TouchCollection touchState;

Accelerometer Accelerometer;

5. 在构造函数添加如下代码,初始化重力感应:

(Code Snippet Game Development with XNA Gameplay Screen Accelerometer Initialization)

C#

Accelerometer = new Accelerometer();

if (Accelerometer.State == SensorState.Ready)

{

Accelerometer.ReadingChanged += (s, e) =>

{

accelState = e;

};

Accelerometer.Start();

}

6. 创建 GameplayScreen 类中的"输入"区域:

C#

#region Input

#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>

public override void HandleInput(InputState input)

{

if (input == null)

throw new ArgumentNullException("input");

if (input.PauseGame)

{

if (gameOver == true)

finishCurrentGame();

}

else

{

touchState = TouchPanel.GetState();

bool buttonTouched = false;

//interpret touch screen presses

foreach (TouchLocation location in touchState)

{

switch (location.State)

{

case TouchLocationState.Pressed:

buttonTouched = true;

break;

case TouchLocationState.Moved:

break;

case TouchLocationState.Released:

break;

}

}

float movement = 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.

KeyboardState keyState = Keyboard.GetState();

if (input.CurrentGamePadStates[0].DPad.Left == ButtonState.Pressed || keyState.IsKeyDown(Keys.Left))

{

//TODO: Update player velocity over X axis #2

}

else if (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

}

else if (gameOver)

finishCurrentGame();

}

}

}

8. 添加Helper方法来完成游戏:

(Code Snippet Game Development with XNA Gameplay Screen finishCurrentGame method)

C#

private void finishCurrentGame()

{

foreach (GameScreen screen in ScreenManager.GetScreens())

screen.ExitScreen();

ScreenManager.AddScreen(new BackgroundScreen());

ScreenManager.AddScreen(new MainMenuScreen());

}

9. 编译应用程序。

在此部分的过程中,您创建一个游戏的输入处理子系统。 它将在创建游戏逻辑的下一个任务中使用。

下一篇就是最后一篇了,你的游戏就要出现了:)

读书人网 >Web前端

热点推荐