
| 動作 | キーボード | Xbox 360 コントローラー | マウス |
|---|---|---|---|
| 入力チェック | - | すべて | - |
※この Tips は XNA Game Studio 2.0 をベースに説明しています。
Xbox 360 では最大4つのコントローラーまで接続することができます。XNA でも4つまでのコントローラーの入力状態を取得することが可能です。他のコントローラーの入力状態を取得するには、「GamePad.GetState」メソッドの第1引数に PlayerIndex 列挙から取得したいプレイヤーインデックスを指定します。
// ゲームパッドの状態を取得する this.gamePadStates[0] = GamePad.GetState(PlayerIndex.One); this.gamePadStates[1] = GamePad.GetState(PlayerIndex.Two); this.gamePadStates[2] = GamePad.GetState(PlayerIndex.Three); this.gamePadStates[3] = GamePad.GetState(PlayerIndex.Four);
サンプルプログラムではあらかじめ4つ分のコントローラーの状態を取得できるように、フィールドで GamePadState の配列を作成しています。
/// <summary> /// ゲームパッドの状態(4プレイヤー分) /// </summary> private GamePadState[] gamePadStates = new GamePadState[4];
Update メソッド内では最初のコードサンプルのように4プレイヤー分のコントローラーの入力状態を取得しています。
Draw メソッド内では以下のメソッドを呼び出して入力情報を画面に表示させています。このメソッド「DrawInputGamePadInformation」は独自に作成したもので、指定座標にコントローラーの入力状態を文字列として表示させます。
// ゲームパッドの入力情報を表示 this.DrawInputGamePadInformation(PlayerIndex.One, new Vector2(50.0f, 50.0f)); this.DrawInputGamePadInformation(PlayerIndex.Two, new Vector2(90.0f, 300.0f)); this.DrawInputGamePadInformation(PlayerIndex.Three, new Vector2(400.0f, 50.0f)); this.DrawInputGamePadInformation(PlayerIndex.Four, new Vector2(450.0f, 300.0f));
下記が「DrawInputGamePadInformation」メソッドのコードになります。
/// <summary> /// ゲームパッドの入力情報を表示する /// </summary> /// <param name="playerIndex">プレイヤーのインデックス</param> /// <param name="textBasePosition">表示するときストの位置</param> private void DrawInputGamePadInformation(PlayerIndex playerIndex, Vector2 textBasePosition) { // ゲームパッドのインデックス int padIndex = (int)playerIndex; string stateMessage; this.spriteBatch.DrawString(this.font, "PlayerIndex : " + playerIndex.ToString(), new Vector2(0.0f, 0.0f) + textBasePosition, Color.White); // 接続状況 this.spriteBatch.DrawString(this.font, "IsConnected : " + this.gamePadStates[padIndex].IsConnected, new Vector2(0.0f, 30.0f) + textBasePosition, Color.White); // 左スティック this.spriteBatch.DrawString(this.font, "LeftStick : " + this.gamePadStates[padIndex].ThumbSticks.Left.X.ToString("f8") + ", " + this.gamePadStates[padIndex].ThumbSticks.Left.Y.ToString("f8"), new Vector2(0.0f, 60.0f) + textBasePosition, Color.White); // 右スティック this.spriteBatch.DrawString(this.font, "RightStick : " + this.gamePadStates[padIndex].ThumbSticks.Right.X.ToString("f8") + ", " + this.gamePadStates[padIndex].ThumbSticks.Right.Y.ToString("f8"), new Vector2(0.0f, 90.0f) + textBasePosition, Color.White); // 方向パッド stateMessage = ""; if (this.gamePadStates[padIndex].DPad.Up == ButtonState.Pressed) { stateMessage += "Up "; } if (this.gamePadStates[padIndex].DPad.Left == ButtonState.Pressed) { stateMessage += "Left "; } if (this.gamePadStates[padIndex].DPad.Down == ButtonState.Pressed) { stateMessage += "Down "; } if (this.gamePadStates[padIndex].DPad.Right == ButtonState.Pressed) { stateMessage += "Right "; } this.spriteBatch.DrawString(this.font, "DirectionalPad : " + stateMessage, new Vector2(0.0f, 120.0f) + textBasePosition, Color.White); stateMessage = ""; // Aボタン if (this.gamePadStates[padIndex].Buttons.A == ButtonState.Pressed) { stateMessage += "A "; } // Bボタン if (this.gamePadStates[padIndex].Buttons.B == ButtonState.Pressed) { stateMessage += "B "; } // Xボタン if (this.gamePadStates[padIndex].Buttons.X == ButtonState.Pressed) { stateMessage += "X "; } // Yボタン if (this.gamePadStates[padIndex].Buttons.Y == ButtonState.Pressed) { stateMessage += "Y "; } // STARTボタン if (this.gamePadStates[padIndex].Buttons.Start == ButtonState.Pressed) { stateMessage += "START "; } // BACKボタン if (this.gamePadStates[padIndex].Buttons.Back == ButtonState.Pressed) { stateMessage += "BACK "; } // Lボタン if (this.gamePadStates[padIndex].Buttons.LeftShoulder == ButtonState.Pressed) { stateMessage += "LB "; } // Rボタン if (this.gamePadStates[padIndex].Buttons.RightShoulder == ButtonState.Pressed) { stateMessage += "RB "; } // LeftStickボタン if (this.gamePadStates[padIndex].Buttons.LeftStick == ButtonState.Pressed) { stateMessage += "LeftStick "; } // RightStickボタン if (this.gamePadStates[padIndex].Buttons.RightStick == ButtonState.Pressed) { stateMessage += "RightStick "; } this.spriteBatch.DrawString(this.font, "Buttons : " + stateMessage, new Vector2(0.0f, 150.0f) + textBasePosition, Color.White); // トリガー this.spriteBatch.DrawString(this.font, "Trigger : " + this.gamePadStates[padIndex].Triggers.Left.ToString("f8") + ", " + this.gamePadStates[padIndex].Triggers.Right.ToString("f8"), new Vector2(0.0f, 180.0f) + textBasePosition, Color.White); }
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_multigamepad_3_0_exe.zip | 9.2 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_multigamepad_2_0_exe.zip | 9.5 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/01 |
| xna_tips_multigamepad_1_0_ref_exe.zip | 9.7 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/08/11 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_multigamepad_3_0_project.zip | 18.4 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_multigamepad_2_0_project.zip | 16.8 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/01 |
| xna_tips_multigamepad_1_0_ref_project.zip | 17.3 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/08/11 |
using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; namespace MultiGamePad { /// <summary> /// ゲームメインクラス /// </summary> public class GameMain : Microsoft.Xna.Framework.Game { /// <summary> /// グラフィックデバイス管理クラス /// </summary> private GraphicsDeviceManager graphics = null; /// <summary> /// スプライトのバッチ化クラス /// </summary> private SpriteBatch spriteBatch = null; /// <summary> /// スプライトでテキストを描画するためのフォント /// </summary> private SpriteFont font = null; /// <summary> /// ゲームパッドの状態(4プレイヤー分) /// </summary> private GamePadState[] gamePadStates = new GamePadState[4]; /// <summary> /// GameMain コンストラクタ /// </summary> public GameMain() { // グラフィックデバイス管理クラ���の作成 this.graphics = new GraphicsDeviceManager(this); // ゲームコンテンツのルートディレクトリを設定 this.Content.RootDirectory = "Content"; } /// <summary> /// ゲームが始まる前の初期化処理を行うメソッド /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う /// </summary> protected override void Initialize() { // TODO: ここに初期化ロジックを書いてください // コンポーネントの初期化などを行います base.Initialize(); } /// <summary> /// ゲームが始まるときに一回だけ呼ばれ /// すべてのゲームコンテンツを読み込みます /// </summary> protected override void LoadContent() { // テクスチャーを描画するためのスプライトバッチクラスを作成します this.spriteBatch = new SpriteBatch(this.GraphicsDevice); // フォントをコンテンツパイプラインから読み込む this.font = this.Content.Load<SpriteFont>("Font"); } /// <summary> /// ゲームが終了するときに一回だけ呼ばれ /// すべてのゲームコンテンツをアンロードします /// </summary> protected override void UnloadContent() { // TODO: ContentManager で管理されていないコンテンツを // ここでアンロードしてください } /// <summary> /// 描画以外のデータ更新等の処理を行うメソッド /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など /// </summary> /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param> protected override void Update(GameTime gameTime) { // ゲームパッドの状態を取得する this.gamePadStates[0] = GamePad.GetState(PlayerIndex.One); this.gamePadStates[1] = GamePad.GetState(PlayerIndex.Two); this.gamePadStates[2] = GamePad.GetState(PlayerIndex.Three); this.gamePadStates[3] = GamePad.GetState(PlayerIndex.Four); // Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます if (this.gamePadStates[0].Buttons.Back == ButtonState.Pressed) { this.Exit(); } // 登録された GameComponent を更新する base.Update(gameTime); } /// <summary> /// 描画処理を行うメソッド /// </summary> /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param> protected override void Draw(GameTime gameTime) { // 画面を指定した色でクリアします this.GraphicsDevice.Clear(Color.CornflowerBlue); // スプライトの描画準備 this.spriteBatch.Begin(); // ゲームパッドの入力情報を表示 this.DrawInputGamePadInformation(PlayerIndex.One, new Vector2(50.0f, 50.0f)); this.DrawInputGamePadInformation(PlayerIndex.Two, new Vector2(90.0f, 300.0f)); this.DrawInputGamePadInformation(PlayerIndex.Three, new Vector2(400.0f, 50.0f)); this.DrawInputGamePadInformation(PlayerIndex.Four, new Vector2(450.0f, 300.0f)); // スプライトの一括描画 this.spriteBatch.End(); // 登録された DrawableGameComponent を描画する base.Draw(gameTime); } /// <summary> /// ゲームパッドの入力情報を表示する /// </summary> /// <param name="playerIndex">プレイヤーのインデックス</param> /// <param name="textBasePosition">表示するときストの位置</param> private void DrawInputGamePadInformation(PlayerIndex playerIndex, Vector2 textBasePosition) { // ゲームパッドのインデックス int padIndex = (int)playerIndex; string stateMessage; this.spriteBatch.DrawString(this.font, "PlayerIndex : " + playerIndex.ToString(), new Vector2(0.0f, 0.0f) + textBasePosition, Color.White); // 接続状況 this.spriteBatch.DrawString(this.font, "IsConnected : " + this.gamePadStates[padIndex].IsConnected, new Vector2(0.0f, 30.0f) + textBasePosition, Color.White); // 左スティック this.spriteBatch.DrawString(this.font, "LeftStick : " + this.gamePadStates[padIndex].ThumbSticks.Left.X.ToString("f8") + ", " + this.gamePadStates[padIndex].ThumbSticks.Left.Y.ToString("f8"), new Vector2(0.0f, 60.0f) + textBasePosition, Color.White); // 右スティック this.spriteBatch.DrawString(this.font, "RightStick : " + this.gamePadStates[padIndex].ThumbSticks.Right.X.ToString("f8") + ", " + this.gamePadStates[padIndex].ThumbSticks.Right.Y.ToString("f8"), new Vector2(0.0f, 90.0f) + textBasePosition, Color.White); // 方向パッド stateMessage = ""; if (this.gamePadStates[padIndex].DPad.Up == ButtonState.Pressed) { stateMessage += "Up "; } if (this.gamePadStates[padIndex].DPad.Left == ButtonState.Pressed) { stateMessage += "Left "; } if (this.gamePadStates[padIndex].DPad.Down == ButtonState.Pressed) { stateMessage += "Down "; } if (this.gamePadStates[padIndex].DPad.Right == ButtonState.Pressed) { stateMessage += "Right "; } this.spriteBatch.DrawString(this.font, "DirectionalPad : " + stateMessage, new Vector2(0.0f, 120.0f) + textBasePosition, Color.White); stateMessage = ""; // Aボタン if (this.gamePadStates[padIndex].Buttons.A == ButtonState.Pressed) { stateMessage += "A "; } // Bボタン if (this.gamePadStates[padIndex].Buttons.B == ButtonState.Pressed) { stateMessage += "B "; } // Xボタン if (this.gamePadStates[padIndex].Buttons.X == ButtonState.Pressed) { stateMessage += "X "; } // Yボタン if (this.gamePadStates[padIndex].Buttons.Y == ButtonState.Pressed) { stateMessage += "Y "; } // STARTボタン if (this.gamePadStates[padIndex].Buttons.Start == ButtonState.Pressed) { stateMessage += "START "; } // BACKボタン if (this.gamePadStates[padIndex].Buttons.Back == ButtonState.Pressed) { stateMessage += "BACK "; } // Lボタン if (this.gamePadStates[padIndex].Buttons.LeftShoulder == ButtonState.Pressed) { stateMessage += "LB "; } // Rボタン if (this.gamePadStates[padIndex].Buttons.RightShoulder == ButtonState.Pressed) { stateMessage += "RB "; } // LeftStickボタン if (this.gamePadStates[padIndex].Buttons.LeftStick == ButtonState.Pressed) { stateMessage += "LeftStick "; } // RightStickボタン if (this.gamePadStates[padIndex].Buttons.RightStick == ButtonState.Pressed) { stateMessage += "RightStick "; } this.spriteBatch.DrawString(this.font, "Buttons : " + stateMessage, new Vector2(0.0f, 150.0f) + textBasePosition, Color.White); // トリガー this.spriteBatch.DrawString(this.font, "Trigger : " + this.gamePadStates[padIndex].Triggers.Left.ToString("f8") + ", " + this.gamePadStates[padIndex].Triggers.Right.ToString("f8"), new Vector2(0.0f, 180.0f) + textBasePosition, Color.White); } } }
| 更新日時 | 更新内容 |
|---|---|
| 2009/01/04 | XNA Game Studio 3.0 のプロジェクト追加 |
| 2008/05/18 | 文章・プログラムの校正 |
| 2008/01/01 | XNA Game Studio 2.0 用に修正 |
| 2007/08/11 | ページ作成 |