Introduction Input Device Keyboard Mouse Game Pad Introduction
大綱 • Introduction • Input Device • Keyboard • Mouse • Game Pad
Introduction Input is a general term referring to the process of receiving actions from the user. In XNA Game Studio, the Microsoft. Xna. Framework. Input namespace provides support for most input devices.
Input Device Input device Windows Xbox 360 Windows Phone Game. Pad Yes ★ Keyboard Yes Yes Mouse Yes Touch. Panel Yes Accelerometer Yes Microphone Yes ★ Only the Back button can be detected on Windows Phone. Yes
Keyboard 相關類別 • Keyboard Class • Get. State • Keyboard. State Structure • Is. Key. Down • Is. Key. Up • Get. Pressed. Keys • Keys Enumeration • Key. State Enumeration • Down • Up
Keys 列舉值 名稱 對應鍵 A–Z 字母鍵 A – Z F 1 – F 24 功能鍵 F 1 – F 24 Num. Pad 0 – Num. Pad 9 數字鍵盤上的 0 – 9 鍵 Left, Right, Up, Down 方向鍵 ← → ↑ ↓ Enter, Tab, Space ENTER 鍵, TAB 鍵, SPACEBAR Escape, Insert, Delete ESP 鍵, INS 鍵, DEL 鍵 Home, End HOME 鍵, END 鍵 Page. Down, Page. Up PAGE DOWN 鍵, PAGE UP 鍵 Left. Control, Right. Control 左 CTRL 鍵, 右 CTRL 鍵 Left. Shift, Right. Shift 左 SHIFT 鍵, 右 SHIFT 鍵 Left. Alt, Right. Alt 左 ALT 鍵, 右 ALT 鍵
偵測鍵盤按鍵的步驟 (Keyboard. Demo. rar) 1. Declare a Keyboard. State object to hold the last known keyboard state. public class Game 1 : Microsoft. Xna. Framework. Game { Graphics. Device. Manager graphics; Sprite. Batch sprite. Batch; Keyboard. State old. KBState;
2. Assign this object a value in your initialization method. protected override void Initialize() { // TODO: Add your initialization logic here old. KBState = Keyboard. Get. State(); base. Initialize(); }
3. Call Get. State to retrieve the current keyboard state protected override void Update(Game. Time game. Time) { // Allows the game to exit if (Game. Pad. Get. State(Player. Index. One). Buttons. Back == Button. State. Pressed) this. Exit(); Keyboard. State new. KBState = Keyboard. Get. State();
4. Compare the values in your new. KBState object to the values in the old. KBState object Keys pressed in the new. KBState object that were not pressed in the old. KBState object were pressed during this frame. Conversely, keys pressed in the old. KBState object that are not pressed in the new. KBState object were released during this frame.
// Is the SPACE key down? if (new. KBState. Is. Key. Down(Keys. Space)) { // If not down last update, key has just been pressed. if (!old. KBState. Is. Key. Down(Keys. Space)) { back. Color = new Color(back. Color. R, back. Color. G, (byte)~back. Color. B); } } else if (old. KBState. Is. Key. Down(Keys. Space)) { // Key was down last update, but not down now, so // it has just been released. }
5. Update old. KBState object to the new. KBState object before leaving Update protected override void Update(Game. Time game. Time) {. . . old. KBState = new. KBState; base. Update(game. Time); }
範例 (Keyboard. Demo 2. rar) protected override void Update(Game. Time game. Time) {. . . Keyboard. State kb. State = Keyboard. Get. State(); if (kb. State. Is. Key. Down(Keys. K)) back. Color = Color. Black; else if (kb. State. Is. Key. Down(Keys. W)) back. Color = Color. White; else if (kb. State. Is. Key. Down(Keys. R)) back. Color = Color. Red; else if (kb. State. Is. Key. Down(Keys. G)) back. Color = Color. Green; else if (kb. State. Is. Key. Down(Keys. B)) back. Color = Color. Blue; else if (kb. State. Is. Key. Down(Keys. Y)) back. Color = Color. Yellow; base. Update(game. Time); }
Mouse 相關類別 • Mouse Class • Get. State • Set. Position • Mouse. State Structure • Left. Button, Middle. Button, Right. Button • XButton 1, XButton 2 • Scroll. Wheel. Value • X, Y • Button. State Enumeration • Pressed • Released • Game. Is. Mouse. Visible
偵測滑鼠的步驟 (Mouse. Demo 1. rar) 1. Call Mouse. Get. State to get the Mouse. State object Mouse. State ms = Mouse. Get. State();
2. Check states of buttons. Mouse. State ms = Mouse. Get. State(); if (ms. Left. Button == Button. State. Pressed) { // Handle left button being pressed } if (ms. Right. Button == Button. State. Released) { // Handle right button being released }
3. Read Mouse. State. X and Mouse. State. Y to get the mouse position, in pixels, relative to the upper-left corner of the game window. Mouse. State ms = Mouse. Get. State(); (0, 0) (ms. X, ms. Y)
Game. Pad Thumb Sticks D-Pad Trigger Button
Xbox 360 Controller Gears of War Controls 1. Left Trigger - Aim, Brake (Vehicle), Pop-out from cover 2. Right Trigger - Shoot, Powerup UV light, Accelerate, Throw (Grenades) 3. Left Shoulder - Review current objectives, locate teammates, give out orders 4. Right Shoulder - Reload weapons
5. Back - Nothing? 6. Start - Pause current game 7. Left Stick - Walk forward, Backstep, Strafe Left, Strafe Right, Move along cover, Crouch (Click), Turn Left (In Vehicles Only), Turn Right (In Vehicles only) 8. D-Pad - Switch between weapons 9. Right Stick - View up, down, left and right, Use Scope (Click)
Xbox Guide Button - Bring up the Xbox 360 Dashboard 10. A - Take Cover, Exit Cover, Roadie Run, Rollover, Attack Order B - Melee, Rev-up Lancer's Chainsaw, Hold Order X - Use, Exit, Kick, Swap Weapon, Pick Up Ammo Y - View point of interests, View fallen teammates, Regroup Order, view emergence holes
Game. Pad 相關類別 • Game. Pad Class • Get. State, Set. Vibration, Get. Capabilities • Game. Pad. State Structure • Buttons, Dpad, Thumb. Sticks, Triggers • Is. Connected, Packet. Number • Game. Pad. Buttons Structure • Game. Pad. DPad Struct • Game. Pad. Thumb. Sticks Structure • Game. Pad. Triggers Structure • Game. Pad. Capabilities Structure • Button. State Enumeration
取得 Game. Pad 的狀態 Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); Player. Index Enumeration One 第一支控制器 Two 第二支控制器 Three 第三支控制器 Four 第四支控制器
檢查 Game. Pad 是否連線? Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); if (gps 1. Is. Connected) { // The gamepad is connected. } else { // The gamepad is discinnected. }
檢查按鈕是否按下? Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); if (gps 1. Buttons. A == Button. State. Pressed) { // Button A is pressed. } Game. Pad. Buttons Properties A B X Y Left. Shoulder Right. Shoulder Left. Stick Right. Stick Start Back Big. Button
檢查 DPad 是否按下? Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); if (gps 1. DPad. Left == Button. State. Pressed) { // The left button of DPad is pressed. } Game. Pad. DPad Properties Left Right Up Down
讀取 Thumb Sticks 的值 Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); Vector 2 left. Thumb. Stick = gps 1. Thumb. Sticks. Left; Vector 2 right. Thumb. Stick = gps 1. Thumb. Sticks. Right; left. Thumb. Stick (right. Thumb. Stick) 是左(右)搖桿的位置 (x, y), x 和 y 的值介 於 -1. 0 和 1. 0 之間。
讀取 Triggers 的值 Game. Pad. State gps 1 = Game. Pad. Get. State(Player. Index. One); float left. Trigger = gps 1. Triggers. Left; float right. Trigger = gps 1. Triggers. Right; left. Trigger (right. Trigger) 是左(右)板機 的(介於 0. 0 和 1. 0 之間)。
檢測 Controller 的功能 Game. Pad. Capabilities gpc 1 = Game. Pad. Get. Capabilities (Player. Index. One); Game. Pad. Capabilities Properties Game. Pad. Type Has. AButton Has. Back. Button Has. Big. Button Has. DPad. Down. Button Has. DPad. Left. Button Has. DPad. Right. Button Has. DPad. Up. Button Has. Left. Shoulder. Button Has. Left. Stick. Button Has. Left. Trigger Has. Left. Vibration. Motor Has. Left. XThumb. Stick Has. Left. YThumb. Stick Has. Right. Shoulder. Button Has. Right. Stick. Button Has. Right. Trigger Has. Right. Vibration. Motor Has. Right. XThumb. Stick Has. Right. YThumb. Stick Has. Start. Button Has. Voice. Support Has. XButton Has. YButton Is. Connected
Game. Pad. Type Enumeration
範例:用 Dpad 來移動物件 (Game. Pad. Dpad. rar) public class Game 1 : Microsoft. Xna. Framework. Game { Graphics. Device. Manager graphics; Sprite. Batch sprite. Batch; Texture 2 D ball; Vector 2 position; Vector 2 velocity;
protected override void Load. Content() { // Create a new Sprite. Batch, which can be used to draw textures. sprite. Batch = new Sprite. Batch(Graphics. Device); // TODO: use this. Content to load your game content here ball = Content. Load<Texture 2 D>("images/ball"); position. X = (Window. Client. Bounds. Width - ball. Width) / 2; position. Y = (Window. Client. Bounds. Height - ball. Height) / 2; velocity = new Vector 2(3); }
protected override void Update(Game. Time game. Time) { Game. Pad. State gps = Game. Pad. Get. State(Player. Index. One); // Allows the game to exit if (gps. Buttons. Back == Button. State. Pressed) this. Exit(); // TODO: Add your update logic here if (gps. DPad. Down == Button. State. Pressed) position. Y += velocity. Y; if (gps. DPad. Up == Button. State. Pressed) position. Y -= velocity. Y; if (gps. DPad. Left == Button. State. Pressed) position. X -= velocity. X; if (gps. DPad. Right == Button. State. Pressed) position. X += velocity. X; base. Update(game. Time); }
protected override void Draw(Game. Time game. Time) { Graphics. Device. Clear(Color. Cornflower. Blue); // TODO: Add your drawing code here sprite. Batch. Begin(); sprite. Batch. Draw(ball, position, Color. White); sprite. Batch. End(); base. Draw(game. Time); }
參考資訊 Web Pages 1. A beginner’s guide to accelerometers. 2. Wiki - Xbox 360 accessories
- Slides: 35