複数のゲームパッドを検出する

Page update date :
Page creation date :

The page you are currently viewing does not support the selected display language.

概要

複数の Xbox 360 コントローラーが接続されているときに、その入力状態を取得します。

複数のゲームパッドを検出する

動作環境

必須環境

対応 XNA バージョン
  • 2.0
  • 3.0
  • 3.1
  • 4.0
対応プラットフォーム
  • Windows (XP SP2 以降, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Windows 必須頂点シェーダ バージョン 2.0
Windows 必須ピクセルシェーダ バージョン 2.0

動作確認環境

プラットフォーム
  • Windows 7
  • Xbox 360
  • Windows Phone 7 エミュレーター

サンプルの操作方法

動作 キーボード Xbox 360 コントローラー マウス タッチ
入力チェック - すべて - -

内容

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 メソッド内では Tips「ゲームパッドの状態を取得する」のように4プレイヤー分のコントローラーの入力状態を取得しています。

Draw メソッド内では以下のメソッドを呼び出して入力情報を画面に表示させています。このメソッド「DrawInputGamePadInformation」は独自に作成したもので、指定座標にコントローラーの入力状態を文字列として表示させます。

// ゲームパッドの入力情報を表示
this.DrawInputGamePadInformation(PlayerIndex.One, new Vector2(20.0f, 20.0f));
this.DrawInputGamePadInformation(PlayerIndex.Two, new Vector2(40.0f, 260.0f));
this.DrawInputGamePadInformation(PlayerIndex.Three, new Vector2(400.0f, 20.0f));
this.DrawInputGamePadInformation(PlayerIndex.Four, new Vector2(420.0f, 260.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);
}

全コード

using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

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";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif
        }

        /// <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);

            // Xbox 360 コントローラ、Windows Phone の 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();

            // ゲームパッドの入力情報を表示
#if WINDOWS_PHONE == false
            this.DrawInputGamePadInformation(PlayerIndex.One, new Vector2(20.0f, 20.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Two, new Vector2(40.0f, 260.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Three, new Vector2(400.0f, 20.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Four, new Vector2(420.0f, 260.0f));
#else
            this.DrawInputGamePadInformation(PlayerIndex.One, new Vector2(10.0f, 0.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Two, new Vector2(70.0f, 200.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Three, new Vector2(10.0f, 400.0f));
            this.DrawInputGamePadInformation(PlayerIndex.Four, new Vector2(70.0f, 600.0f));
#endif

            // スプライトの一括描画
            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);
        }
    }
}