■XNAで入力(マウス) Prev  Top  Next

次にマウス入力です。簡単なのでさくっと。


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 MouseState mouseState = Mouse.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");

         //ウインドウ上でマウスポインタを表示するようにする。デフォルトで非表示。
         this.IsMouseVisible = true;

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

         //マウスの状態を取得
         mouseState = Mouse.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();

         //マウスの左ボタンをクリックしている
         if( mouseState.LeftButton == ButtonState.Pressed )
            this.spriteBatch.DrawString(this.font, "Push Left", new Vector2(0,0), Color.White);

         //マウスの右ボタンをクリックしている
         if (mouseState.RightButton == ButtonState.Pressed)
            this.spriteBatch.DrawString(this.font, "Push Right", new Vector2(0, 20), Color.White);

         //マウスポインタの位置
         this.spriteBatch.DrawString(this.font, "X:" + mouseState.X.ToString(), new Vector2(100, 00), Color.White);
         this.spriteBatch.DrawString(this.font, "Y:" + mouseState.Y.ToString(), new Vector2(100, 20), Color.White);

         // ホイールの回転量
         this.spriteBatch.DrawString(this.font, "ScrollWheelValue:" + this.mouseState.ScrollWheelValue, new Vector2(200.0f, 0.0f), Color.White);

         this.spriteBatch.End();

         base.Draw(gameTime);
      }
   }
}


マウスの状態を表示しています。特に解説することもないと思います。

Prev  Top  Next
inserted by FC2 system