■XNAで入力(ジョイパッド) | Prev Top Next |
ジョイパッドで入力を行います。使用するジョイパッドはXBOX360用のジョイパッドです。
namespace Tutrial { /// /// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics = null; private ContentManager content = null; //**************************************************************** //オブジェクトの定義 //**************************************************************** private SpriteBatch spriteBatch = null; //スプライトオブジェクト private SpriteFont font = null; //フォントオブジェクト private GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); //1つめのコントローラー public Game1() { this.graphics = new GraphicsDeviceManager(this); //リソースフォルダのルートパスを指定 this.content = new ContentManager(this.Services, "Resource"); } /// /// 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. /// protected override void Initialize() { //**************************************************************** //オブジェクトの初期化 //**************************************************************** //スプライトの初期化 this.spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice); //フォントを読み込む this.font = this.content.Load("Font"); base.Initialize(); } /// /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content } // TODO: Load any ResourceManagementMode.Manual content } ////// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } } ////// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); //1つめのコントローラー gamePadState = GamePad.GetState(PlayerIndex.One); base.Update(gameTime); } ////// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { //背景色をグレーで塗りつぶす this.graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Vector4(0.2f, 0.2f, 0.2f, 1.0f), 1.0f, 0 ); this.spriteBatch.Begin(); //コントローラーがコネクトされている(注意1) if (gamePadState.IsConnected == true) { this.spriteBatch.DrawString(this.font, "Connect", new Vector2(0, 0), Color.White); //+字キー(上)を入力 if (gamePadState.DPad.Up == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "DPad Up", new Vector2(100, 0), Color.White); //+字キー(下)を入力 if (gamePadState.DPad.Down == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "DPad Down", new Vector2(100, 15), Color.White); //+字キー(左)を入力 if( gamePadState.DPad.Left == ButtonState.Pressed ) this.spriteBatch.DrawString(this.font, "DPad Left", new Vector2(100, 30), Color.White); //+字キー(右)を入力 if (gamePadState.DPad.Right == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "DPad Right", new Vector2(100, 45), Color.White); //Aボタンを入力 if (gamePadState.Buttons.A == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "A", new Vector2(200, 0), Color.White); //Bボタンを入力 if (gamePadState.Buttons.B == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "B", new Vector2(200, 15), Color.White); //Xボタンを入力 if (gamePadState.Buttons.X == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "X", new Vector2(200, 30), Color.White); //Yボタンを入力 if (gamePadState.Buttons.Y == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "Y", new Vector2(200, 45), Color.White); //LBボタンを入力 if (gamePadState.Buttons.LeftShoulder == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "LB", new Vector2(200, 60), Color.White); //RBボタンを入力 if (gamePadState.Buttons.RightShoulder == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "RB", new Vector2(200, 75), Color.White); //Startボタンを入力 if (gamePadState.Buttons.Start == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "Start", new Vector2(200, 90), Color.White); //Backボタンを入力 if (gamePadState.Buttons.Back == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "Back", new Vector2(200, 105), Color.White); //スティック(左)ボタンを入力 if (gamePadState.Buttons.LeftStick == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "LeftStickPush", new Vector2(200, 90), Color.White); //スティック(右)ボタンを入力 if (gamePadState.Buttons.RightStick == ButtonState.Pressed) this.spriteBatch.DrawString(this.font, "RightStickPush", new Vector2(200, 105), Color.White); //トリガー(左)ボタンを入力 this.spriteBatch.DrawString(this.font, "Triggers Left:" + gamePadState.Triggers.Left.ToString("f6"), new Vector2(400, 0), Color.White); //トリガー(右)ボタンを入力 this.spriteBatch.DrawString(this.font, "Triggers Right:" + gamePadState.Triggers.Right.ToString("f6"), new Vector2(400, 15), Color.White); //スティック(左)の傾き this.spriteBatch.DrawString(this.font, "LeftStick X:" + this.gamePadState.ThumbSticks.Left.X.ToString("f6"), new Vector2(600, 0), Color.White); this.spriteBatch.DrawString(this.font, "LeftStick Y:" + this.gamePadState.ThumbSticks.Left.Y.ToString("f6"), new Vector2(600, 15), Color.White); //スティック(右)の傾き this.spriteBatch.DrawString(this.font, "RightStick X:" + this.gamePadState.ThumbSticks.Right.X.ToString("f6"), new Vector2(600, 30), Color.White); this.spriteBatch.DrawString(this.font, "RightStick Y:" + this.gamePadState.ThumbSticks.Right.Y.ToString("f6"), new Vector2(600, 45), Color.White); } //コントローラーがコネクトされていない else this.spriteBatch.DrawString(this.font, "Not Connect", new Vector2(0, 0), Color.White); this.spriteBatch.End(); base.Draw(gameTime); } } }
ジョイパッドの入力状態を表示しています。
(注意1) XBox 360のジョイパッドでテストしました。これ以外のジョイパッドではGamePadState::IsConnectedがfalseを返します。 どうもXBox 360用のジョイパッドしか使用できないようですが。