protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } var keyboardState = Keyboard.GetState(); var elapsedTime = (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (keyboardState.IsKeyDown(Keys.Right)) { MoveMario(new Vector2(5, 0), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Right); } else if (keyboardState.IsKeyDown(Keys.Left)) { MoveMario(new Vector2(-5, 0), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Left); } else if (keyboardState.IsKeyDown(Keys.Up)) { MoveMario(new Vector2(0, -5), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Up); } else if (keyboardState.IsKeyDown(Keys.Down)) { MoveMario(new Vector2(0, 5), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Down); } if (keyboardState.IsKeyUp(Keys.Right) && keyboardState.IsKeyUp(Keys.Left) && keyboardState.IsKeyUp(Keys.Up) && keyboardState.IsKeyUp(Keys.Down)) { _marioSprite.Reset(); } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } var keyboardState = Keyboard.GetState(); var elapsedTime = (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (keyboardState.IsKeyDown(Keys.Right)) { MoveMario(new Vector2(5, 0), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Right); } else if (keyboardState.IsKeyDown(Keys.Left)) { MoveMario(new Vector2(-5, 0), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Left); } else if (keyboardState.IsKeyDown(Keys.Up)) { MoveMario(new Vector2(0, -5), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Up); } else if (keyboardState.IsKeyDown(Keys.Down)) { MoveMario(new Vector2(0, 5), elapsedTime); _marioSprite.SetAnimationRow((int)FacingDirection.Down); } if (keyboardState.IsKeyUp(Keys.Right) && keyboardState.IsKeyUp(Keys.Left) && keyboardState.IsKeyUp(Keys.Up) && keyboardState.IsKeyUp(Keys.Down)) { // Todo: // Call the Reset method of the _marioSprite instance so that the current frame resets back to 0 when no keys are pressed. } base.Update(gameTime); }