
| 動作 | キーボード | Xbox 360 コントローラー | マウス |
|---|---|---|---|
| 押下チェック | - | - | 左ボタン |
XNA Framework の標準機能ではマウスののボタンを押した瞬間を取得する機能はないので、自分で実装する必要があります。実装方法としては、以前のボタンの押下情報を保持しておいて現在の押下状態と比較し、前回が押されてなく現在押されていた場合は押した瞬間と認識させます。
サンプルはマウスの左ボタンの例で説明しています。フィールドに前回の押下状態と現在の押下状態を保持できるように bool で宣言しています。押した回数を数えるためのカウンターも用意しています。
/// <summary> /// 現在マウスの左ボタンを押しているか /// </summary> private bool nowMouseButtonPushed = false; /// <summary> /// 前回マウスの左ボタンを押しているか /// </summary> private bool previousMouseButtonPushed = false; /// <summary> /// ボタンが押された回数 /// </summary> private int pushedCount = 0;
Update メソッド内では前回取得したボタンの押下状態を記憶し、現在のボタンの状態を取得しています。もし、前回押されていなく現在押されていた場合は、ボタンを押した瞬間と判断しカウントしています。
// 前回のマウスボタンの押下状態を記憶 this.previousMouseButtonPushed = this.nowMouseButtonPushed; // マウスの状態を取得 MouseState mouseState = Mouse.GetState(); // 現在のマウスボタンの押下状態を記憶 this.nowMouseButtonPushed = mouseState.LeftButton == ButtonState.Pressed; // ボタンが押された瞬間ならカウントする if (this.nowMouseButtonPushed && !this.previousMouseButtonPushed) { this.pushedCount++; }
Xbox 360 ではマウス関連のプログラムを記述することはできますが、マウス自体使用することはできないため、正常な値を取得することはできません。
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_justpushedmousebutton_3_0_exe.zip | 8.5 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_justpushedmousebutton_2_0_exe.zip | 8.8 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/01 |
| xna_tips_justpushedmousebutton_1_0_ref_exe.zip | 9.0 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/07/25 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_justpushedmousebutton_3_0_project.zip | 19.3 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_justpushedmousebutton_2_0_project.zip | 16.1 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/01 |
| xna_tips_justpushedmousebutton_1_0_ref_project.zip | 16.3 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/07/25 |
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.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace JustPushedMouseButton { /// <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> /// 現在マウスの左ボタンを押しているか /// </summary> private bool nowMouseButtonPushed = false; /// <summary> /// 前回マウスの左ボタンを押しているか /// </summary> private bool previousMouseButtonPushed = false; /// <summary> /// ボタンが押された回数 /// </summary> private int pushedCount = 0; /// <summary> /// GameMain コンストラクタ /// </summary> public GameMain() { // グラフィックデバイス管理クラスの作成 this.graphics = new GraphicsDeviceManager(this); // ゲームコンテンツのルートディレクトリを設定 this.Content.RootDirectory = "Content"; // ウインドウ上でマウスのポインタを表示するようにする this.IsMouseVisible = true; } /// <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) { // Xbox 360 コントローラの BACK ボタンを押したときにゲームを終了させます if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // 前回のマウスボタンの押下状態を記憶 this.previousMouseButtonPushed = this.nowMouseButtonPushed; // マウスの状態を取得 MouseState mouseState = Mouse.GetState(); // 現在のマウスボタンの押下状態を記憶 this.nowMouseButtonPushed = mouseState.LeftButton == ButtonState.Pressed; // ボタンが押された瞬間ならカウントする if (this.nowMouseButtonPushed && !this.previousMouseButtonPushed) { this.pushedCount++; } // 登録された 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.spriteBatch.DrawString(this.font, "Please press a left button of the mouse.", new Vector2(50.0f, 50.0f), Color.White); // ボタンが押された回数を表示 this.spriteBatch.DrawString(this.font, "Pushed Count : " + this.pushedCount.ToString(), new Vector2(50.0f, 80.0f), Color.White); // マウスの左ボタンが押されている場合は文字を表示する if (this.nowMouseButtonPushed) { this.spriteBatch.DrawString(this.font, "The left button of the mouse is pressed.", new Vector2(50.0f, 110.0f), Color.Yellow); } // スプライトの一括描画 this.spriteBatch.End(); // 登録された DrawableGameComponent を描画する base.Draw(gameTime); } } }
| 更新日時 | 更新内容 |
|---|---|
| 2009/01/04 | XNA Game Studio 3.0 用に修正 |
| 2008/05/18 | 文章・プログラムの校正 |
| 2008/01/12 | 対象プラットフォームに「Xbox 360」が含まれていたのを削除 |
| 2008/01/01 | XNA Game Studio 2.0 用に修正 |
| 2007/07/25 | ページ作成 |