要取得第一個把手觸發的按鈕,可以宣告一個GamePadState類別,並且透過GamePad的GetState去取得狀態
GamePad的GetState的方法可以帶入指定的手把編號,手把編號可以透過PlayerIndex列舉去指定
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
當第一個手把按下手把上的按鈕,就會將程式標題改成按鈕名稱
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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
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
}
/// <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
}
/// <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();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
//開始鈕
if (gamePadState.Buttons.Start == ButtonState.Pressed)
{
this.Window.Title = "Start";
}
//A
if (gamePadState.Buttons.A == ButtonState.Pressed)
{
this.Window.Title = "A";
}
//B
if (gamePadState.Buttons.B == ButtonState.Pressed)
{
this.Window.Title = "B";
}
//X
if (gamePadState.Buttons.X == ButtonState.Pressed)
{
this.Window.Title = "X";
}
//Y
if (gamePadState.Buttons.Y == ButtonState.Pressed)
{
this.Window.Title = "Y";
}
//LB
if (gamePadState.Buttons.LeftShoulder == ButtonState.Pressed)
{
this.Window.Title = "LeftShoulder";
}
//LF
if (gamePadState.Buttons.RightShoulder == ButtonState.Pressed)
{
this.Window.Title = "RightShoulder";
}
//左搖桿
if (gamePadState.Buttons.LeftStick == ButtonState.Pressed)
{
this.Window.Title = "LeftStick";
}
//右搖桿
if (gamePadState.Buttons.RightStick == ButtonState.Pressed)
{
this.Window.Title = "RightStick";
}
//十字鍵 下
if (gamePadState.DPad.Down == ButtonState.Pressed)
{
this.Window.Title = "Down";
}
//十字鍵 上
if (gamePadState.DPad.Up == ButtonState.Pressed)
{
this.Window.Title = "Up";
}
//十字鍵 左
if (gamePadState.DPad.Left == ButtonState.Pressed)
{
this.Window.Title = "Left";
}
//十字鍵 右
if (gamePadState.DPad.Right == ButtonState.Pressed)
{
this.Window.Title = "Right";
}
// 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
base.Draw(gameTime);
}
}
}
參考資料:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.gamepadstate.aspx
http://msdn.microsoft.com/en-us/library/bb203900.aspx
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.gamepadstate_members.aspx


