INPUT(キーボード編)
■そろそろ入力してみませんか
X360でUSBキーボードをさすと動くかどうかテストをしてませんが、Windowsでは動作した物を公開します。
namespace WindowsGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; private SpriteBatch sprite; private Texture2D texture; private Vector2 myPos = new Vector2( 0, 0 ); public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { sprite = new SpriteBatch(graphics.GraphicsDevice); texture = content.Load<Texture2D>("mimi"); } } protected override void Update(GameTime gameTime) { keybordInput(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); sprite.Begin(); sprite.Draw(texture, myPos , Color.White); sprite.End(); base.Draw(gameTime); } /// <summary> /// キーボード入力 /// </summary> private void keybordInput() { KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Right)) { // 右ボタンが押された myPos.X += 1.0f; } if (keyState.IsKeyDown(Keys.Left)) { // 左ボタンが押された myPos.X -= 1.0f; } if (keyState.IsKeyDown(Keys.Up)) { // 上ボタンが押された myPos.Y -= 1.0f; } if (keyState.IsKeyDown(Keys.Down)) { // 下ボタンが押された myPos.Y += 1.0f; } } } }
■ソース説明
キー情報を取得します。
KeyboardState keyState = Keyboard.GetState();
Keysにより、押されたキーを確認しよう。
if (keyState.IsKeyDown(Keys.*))
■実行
キーボードの上下左右キーを押してキャラクターを操作する事がわかると思います。