■XNAで入力(キーボード) Prev  Top  Next

今回から入力インターフェースを解説します。これをいれて初めてキャラクターを操作できるようになります。

なお、PCゲームの場合、XBOX 360と異なりユーザーの環境がさまざまです。入力インターフェースにキーボードとマウスしか用意してない場合もあるでしょう。なのでキーボードによる入力操作は必ず対応しておいたおうがよいでしょう。


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 KeyboardState keyState = Keyboard.GetState(); //キーボード入力状態

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

         //キーボードの押下状態を取得
         keyState = Keyboard.GetState();

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

         //****************************************************************
         //押されているキーの番号を表示する(注意2)
         //****************************************************************
         
         for (int i = (int)Keys.None; i <= (int)Keys.OemClear; i++)
         {
            if (this.keyState.IsKeyDown((Keys)i) == true)
               this.spriteBatch.DrawString(this.font, i.ToString(), new Vector2(i / 20 * 30, i % 20 * 20), Color.White);
         }

         this.spriteBatch.End();

         base.Draw(gameTime);
      }
   }
}


押下されているキーの番号を画面上に表示してます。それだけで面白くもなんともありません。(笑)

(注意1) Update関数について。英語のコメントを翻訳すると「それぞれの更新処理、当たり判定、入力制御、オーディオ制御を行うときのロジックを記述してください。」みたいな感じになります。(間違ってたらごめんなさい。)
要するに描画以外の更新処理をすべてここに入れとけということです。Update関数は毎回処理されます。 これに対しDraw関数は1フレームごとに処理されます(デフォルトでは1フレーム = 1 / 60秒間に設定)。 そして重要なのは描画処理に遅延が発生したときフレームのスキップ処理を行いことです。したがって入力制御などをDraw関数内で処理するように設計すると、 描画処理で遅延が発生した場合、すべての入力チェックが行えないという問題が発生します。確実に入力チェックを行うためにUpdate関数内で処理するようにしましょう。

(注意2) Keysは列挙型です。したがって内部的にはただの数字です。番号が知りたいときは、NoneOemClearに カーソルを合わせて、右クリックして、「定義へ移動」をクリックしてください。

Prev  Top  Next
inserted by FC2 system