本文整理汇总了C#中Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown方法的典型用法代码示例。如果您正苦于以下问题:C# KeyboardState.IsKeyDown方法的具体用法?C# KeyboardState.IsKeyDown怎么用?C# KeyboardState.IsKeyDown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Input.KeyboardState
的用法示例。
在下文中一共展示了KeyboardState.IsKeyDown方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Update
//Update Method
public void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState(); //create a keyboard state variable to hold current keyboard state
if (keyState.IsKeyDown(Keys.Back) && keyState.IsKeyDown(Keys.Back))
{
gameState.CurrentScreen = Screen.StartScreen;
}
lastState = keyState;
if (keyState.IsKeyDown(Keys.S) && lastState.IsKeyDown(Keys.S)) // if s key is pressed
{
gameState.CurrentScreen = Screen.StartScreen; // switches to start screen
}
if (keyState.IsKeyDown(Keys.G) && lastState.IsKeyDown(Keys.G)) // if G key is pressed
{
gameState.StartGame("test.txt", "level2"); // calls start game method to switch to game
}
if (keyState.IsKeyDown(Keys.O) && lastState.IsKeyDown(Keys.O))
{
gameState.SwitchOption(game);//calls switch option method to switch to option screen
}
if (keyState.IsKeyDown(Keys.L) && lastState.IsKeyDown(Keys.L))
{
gameState.SwitchLevel(game);
}
if (keyState.IsKeyDown(Keys.I) && lastState.IsKeyDown(Keys.I))
{
gameState.SwitchInstruct(game);
}
lastState = keyState; // assigns current keyboard state to the last keyboard state
}
示例2: Handler
public static void Handler(KeyboardState KeyState, GameTime gameTime)
{
float _speed = 5;
//Clear attack state on every update
_attack = false;
if (KeyState.IsKeyDown(Keys.W) | KeyState.IsKeyDown(Keys.Up))
{
Player.hiLocation.Y = Player.hiLocation.Y - _speed;
//Move up
}
if (KeyState.IsKeyDown(Keys.A) | KeyState.IsKeyDown(Keys.Left))
{
//Move left
Player.hiLocation.X = Player.hiLocation.X - _speed;
}
if (KeyState.IsKeyDown(Keys.S) | KeyState.IsKeyDown(Keys.Down))
{
//Move down
Player.hiLocation.Y = Player.hiLocation.Y + _speed;
}
if (KeyState.IsKeyDown(Keys.D) | KeyState.IsKeyDown(Keys.Right))
{
//Move right
Player.hiLocation.X = Player.hiLocation.X + _speed;
}
if (KeyState.IsKeyDown(Keys.Space))
{
//Attack
if (attackHold <= 250 && attackWait <= 0)
{
//If not held attack
_attack = true;
attackHold++;
timer = ((int)250 - (int)attackHold).ToString();
}
if(attackHold >= 250)
{
//If held to long block attack for 125 frames
attackHold = 0;
attackWait = 125;
timer = "125C";
}
}
if(KeyState.IsKeyUp(Keys.Space) && attackWait <= 0 || PositionChecker.dead)
{
//Reset hold and pause on key up
attackHold = 0;
attackWait = 0;
timer = "250";
}
if (attackWait > 0)
{
timer = String.Format("{0}C", attackWait.ToString());
attackWait--;
}
if (KeyState.IsKeyDown(Keys.F1) && Game1.debug)
Game1.spawn = !Game1.spawn;
//Validate new position
PositionChecker.PosChecker();
}
示例3: Update
public void Update(KeyboardState keyboard, MouseState mouse, Engine engine)
{
// Rortational Origin
Rectangle originRect = new Rectangle((int) position.X, (int) position.Y,
(int) texture.Width, (int) texture.Height);
origin = new Vector2(originRect.Width / 2, originRect.Height / 2);
int playerSpeed = 2;
if (keyboard.IsKeyDown(Keys.A))
position.X -= playerSpeed;
if (keyboard.IsKeyDown(Keys.D))
position.X += playerSpeed;
if (keyboard.IsKeyDown(Keys.W))
position.Y -= playerSpeed;
if (keyboard.IsKeyDown(Keys.S))
position.Y += playerSpeed;
float deltaY = mouse.Y - position.Y;
float deltaX = mouse.X - position.X;
float radians = (float) Math.Atan2(deltaY, deltaX);
setRotation(radians);
}
示例4: Update
public void Update()
{
_curState = Keyboard.GetState();
KeyLeftPressing = false;
KeyRightPressing = false;
KeyUpPressing = false;
KeyDownPressing = false;
if (_curState.IsKeyDown(Keys.A))
KeyLeftPressing = true;
else if (_curState.IsKeyDown(Keys.D))
KeyRightPressing = true;
if (_curState.IsKeyDown(Keys.W))
{
KeyUpPressing = true;
KeyUpPressed = _prevState.IsKeyUp(Keys.W);
}
else if (_curState.IsKeyDown(Keys.S))
{
KeyDownPressing = true;
KeyDownPressed = _prevState.IsKeyUp(Keys.S);
}
KeyJumpPressed = IsKeyClick(Keys.Space);
KeyAttackPressed = IsKeyClick(Keys.J);
KeySecondaryPressed = IsKeyClick(Keys.K);
KeyStartPressed = IsKeyClick(Keys.Enter);
_prevState = _curState;
}
示例5: ProcessController
public void ProcessController()
{
keyState = Keyboard.GetState();
padState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
Actor hero = (Actor)game.Hero;
/*
* GamePad inputs
*/
//Moving
if (padState.IsButtonDown(Buttons.LeftThumbstickLeft) || keyState.IsKeyDown(Keys.Left))
{
hero.Pos.X -= 1.5f;
hero.NextPosture(ActorDirection.Left);
}
if (padState.IsButtonDown(Buttons.LeftThumbstickRight) || keyState.IsKeyDown(Keys.Right))
{
hero.Pos.X += 1.5f;
hero.NextPosture(ActorDirection.Right);
}
//Jump
if (padState.IsButtonDown(Buttons.LeftThumbstickUp))
{
}
if (padState.IsButtonDown(Buttons.LeftThumbstickDown))
{
}
}
示例6: Update
internal override void Update(GameTime gameTime)
{
NeedDraw = true;
NeedUpdate = true;
currentKey = Keyboard.GetState();
if (currentKey.IsKeyDown(Keys.D1) && prevKey.IsKeyUp(Keys.D1))
{
_GLOBAL.GameStateManager.activate(new Sample1());
NeedDraw = false;
NeedUpdate = false;
}
else if (currentKey.IsKeyDown(Keys.D2) && prevKey.IsKeyUp(Keys.D2))
{
_GLOBAL.GameStateManager.activate(new Sample2());
NeedDraw = false;
NeedUpdate = false;
}
else if (currentKey.IsKeyDown(Keys.Escape) && prevKey.IsKeyUp(Keys.Escape))
{
_GLOBAL.GameStateManager.activate(new Opening());
NeedDraw = false;
NeedUpdate = false;
_GLOBAL.inGameState = false;
}
prevKey = currentKey;
}
示例7: CheckKey
static public void CheckKey(KeyboardState keyboardState)
{
changed = true;
if (keyboardState.IsKeyDown(Keys.Up))
{
if (++characters[characterIndex] > 'Z')
characters[characterIndex] = 'A';
} else if (keyboardState.IsKeyDown(Keys.Down))
{
if (--characters[characterIndex] < 'A')
characters[characterIndex] = 'Z';
} else if (keyboardState.IsKeyDown(Keys.Left))
{
if (--characterIndex < 0)
characterIndex = characterNumber - 1;
} else if (keyboardState.IsKeyDown(Keys.Right))
{
if (++characterIndex > characterNumber - 1)
characterIndex = 0;
}
}
示例8: Update
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
keyBoardState = Keyboard.GetState();
//Перемещение объекта
if (keyBoardState.IsKeyDown(Keys.Up))
position.Y -= 5;
if (keyBoardState.IsKeyDown(Keys.Down))
position.Y += 5;
if (keyBoardState.IsKeyDown(Keys.Left))
position.X -= 5;
if (keyBoardState.IsKeyDown(Keys.Right))
position.X += 5;
//Рамки экрана
if (position.X <= 0)
position.X = 0;
if (position.Y <= 0)
position.Y = 0;
if (position.Y > graphics.PreferredBackBufferHeight -vader.Height)
position.Y = graphics.PreferredBackBufferHeight - vader.Height;
if (position.X > graphics.PreferredBackBufferWidth - vader.Width)
position.X = graphics.PreferredBackBufferWidth - vader.Width;
base.Update(gameTime);
}
示例9: UpdateMovement
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
mSpeed.X = NERD_SPEED;
mDirection.X = MOVE_LEFT;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
mSpeed.X = NERD_SPEED;
mDirection.X = MOVE_RIGHT;
}
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
mSpeed.Y = NERD_SPEED;
mDirection.Y = MOVE_UP;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
mSpeed.Y = NERD_SPEED;
mDirection.Y = MOVE_DOWN;
}
}
}
示例10: UpdateMovement
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
Left = !Left;
Right = false;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
Right = !Right;
Left = false;
}
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
Up = !Up;
Down = false;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
Down = !Down;
Up = false;
}
//Reset movement to not moving
if (aCurrentKeyboardState.IsKeyUp(Keys.Down) == true)
{
Down = false;
}
if (aCurrentKeyboardState.IsKeyUp(Keys.Up) == true)
{
Up = false;
}
if (aCurrentKeyboardState.IsKeyUp(Keys.Left) == true)
{
Left = false;
}
if (aCurrentKeyboardState.IsKeyUp(Keys.Right) == true)
{
Right = false;
}
//Update movement
if (Right)
rectangle.X++;
if (Left)
rectangle.X--;
if (Up)
rectangle.Y--;
if (Down)
rectangle.Y++;
}
示例11: InputManager
/// <summary>
/// Handles where to send what control to send the Input to, either
/// a character, or a menu etc
/// </summary>
/// <param name="state">Where to send the input.</param>
public void InputManager(GameState state)
{
kbState = Keyboard.GetState();
//if the game is in the main gameplay mode
if (state.GetType() == typeof(GameStateMain))
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
else if (kbState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
else if (kbState.IsKeyDown(Keys.F) && oldKbState.IsKeyUp(Keys.F))
{
FeedAction feeding = new FeedAction(session.user);
screenBG = Color.Aqua;
feeding.FeedPet(session.pet, new FoodFruit());
}
else if (kbState.IsKeyDown(Keys.Space) && oldKbState.IsKeyUp(Keys.Space))
{
mSprite.GoToNextFrame();
}
}
oldKbState = kbState;
}
示例12: FromInput
/// <summary>
/// Gets the current direction from a game pad and keyboard.
/// </summary>
public static Buttons FromInput(GamePadState gamePad, KeyboardState keyboard)
{
Buttons direction = None;
// Get vertical direction.
if (gamePad.IsButtonDown(Buttons.DPadUp) ||
gamePad.IsButtonDown(Buttons.LeftThumbstickUp) ||
keyboard.IsKeyDown(Keys.Up))
{
direction |= Up;
}
else if (gamePad.IsButtonDown(Buttons.DPadDown) ||
gamePad.IsButtonDown(Buttons.LeftThumbstickDown) ||
keyboard.IsKeyDown(Keys.Down))
{
direction |= Down;
}
// Comebine with horizontal direction.
if (gamePad.IsButtonDown(Buttons.DPadLeft) ||
gamePad.IsButtonDown(Buttons.LeftThumbstickLeft) ||
keyboard.IsKeyDown(Keys.Left))
{
direction |= Left;
}
else if (gamePad.IsButtonDown(Buttons.DPadRight) ||
gamePad.IsButtonDown(Buttons.LeftThumbstickRight) ||
keyboard.IsKeyDown(Keys.Right))
{
direction |= Right;
}
return direction;
}
示例13: HandleKeyboardInput
private void HandleKeyboardInput(KeyboardState keyState)
{
if (keyState.IsKeyDown(Keys.Up))
{
playerSprite.Velocity += new Vector2(0, -1);
}
if (keyState.IsKeyDown(Keys.Down))
{
playerSprite.Velocity += new Vector2(0, 1);
}
if (keyState.IsKeyDown(Keys.Left))
{
playerSprite.Velocity += new Vector2(-1, 0);
}
if (keyState.IsKeyDown(Keys.Right))
{
playerSprite.Velocity += new Vector2(1, 0);
}
if (keyState.IsKeyDown(Keys.Space))
{
FireShot();
}
}
示例14: Update
public void Update(KeyboardState keyboard)
{
if (keyboard.IsKeyDown(up))
{
player.MoveUp();
}
else if (keyboard.IsKeyDown(down))
{
player.MoveDown();
}
else if (keyboard.IsKeyDown(left))
{
player.MoveLeft();
}
else if (keyboard.IsKeyDown(right))
{
player.MoveRight();
}
else
{
player.MoveStop();
}
if (keyboard.IsKeyDown(run))
{
player.Run();
}
else
{
player.Walk();
}
}
示例15: HandleInput
protected virtual void HandleInput(GameTime gameTime)
{
keyState = Keyboard.GetState();
Vector2 force = Vector2.Zero;
if (keyState.IsKeyDown(Keys.A))
{
force.X -= forcePower * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (keyState.IsKeyDown(Keys.D))
{
force.X += forcePower * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (keyState.IsKeyDown(Keys.W))
{
force.Y -= forcePower * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (keyState.IsKeyDown(Keys.S))
{
force.Y += forcePower * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
body.ApplyLinearImpulse(force, body.Position);
//oldState = keyState;
}