From a3122d1e1c78c2ca3d6edef745fb3fa4d0c5b29f Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 26 Jul 2026 20:33:30 -0400 Subject: [PATCH] Fix an input updates delay of one frame Previously, CheckKeyboardInput() and CheckGamePadInput() were called before the call to base.Update(), resulting in stale input data. This sequence results in stale input data because the base.Update() call is where InputManager is updated with fresh input data, and because the "CheckInput" methods get their information from the InputManager. The corresponding fix simply moved the update ahead of the checker methods. --- .../building_2d_games/11_input_management/snippets/game1.cs | 5 +++-- .../12_collision_detection/snippets/game1.cs | 5 +++-- .../13_working_with_tilemaps/snippets/game1.cs | 5 +++-- .../14_soundeffects_and_music/snippets/game1.cs | 5 +++-- .../building_2d_games/15_audio_controller/snippets/game1.cs | 5 +++-- .../16_working_with_spritefonts/snippets/game1.cs | 5 +++-- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/articles/tutorials/building_2d_games/11_input_management/snippets/game1.cs b/articles/tutorials/building_2d_games/11_input_management/snippets/game1.cs index af9fa1b7..81211f19 100644 --- a/articles/tutorials/building_2d_games/11_input_management/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/11_input_management/snippets/game1.cs @@ -50,6 +50,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -61,8 +64,6 @@ protected override void Update(GameTime gameTime) // Check for gamepad input and handle it. CheckGamePadInput(); - - base.Update(gameTime); } private void CheckKeyboardInput() diff --git a/articles/tutorials/building_2d_games/12_collision_detection/snippets/game1.cs b/articles/tutorials/building_2d_games/12_collision_detection/snippets/game1.cs index d92dab21..414e1547 100644 --- a/articles/tutorials/building_2d_games/12_collision_detection/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/12_collision_detection/snippets/game1.cs @@ -61,6 +61,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -175,8 +178,6 @@ protected override void Update(GameTime gameTime) // Assign a new random velocity to the bat AssignRandomBatVelocity(); } - - base.Update(gameTime); } private void AssignRandomBatVelocity() diff --git a/articles/tutorials/building_2d_games/13_working_with_tilemaps/snippets/game1.cs b/articles/tutorials/building_2d_games/13_working_with_tilemaps/snippets/game1.cs index e5535c60..f5c4e8ce 100644 --- a/articles/tutorials/building_2d_games/13_working_with_tilemaps/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/13_working_with_tilemaps/snippets/game1.cs @@ -84,6 +84,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -185,8 +188,6 @@ protected override void Update(GameTime gameTime) // Assign a new random velocity to the bat AssignRandomBatVelocity(); } - - base.Update(gameTime); } private void AssignRandomBatVelocity() diff --git a/articles/tutorials/building_2d_games/14_soundeffects_and_music/snippets/game1.cs b/articles/tutorials/building_2d_games/14_soundeffects_and_music/snippets/game1.cs index 9069610f..7467e843 100644 --- a/articles/tutorials/building_2d_games/14_soundeffects_and_music/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/14_soundeffects_and_music/snippets/game1.cs @@ -113,6 +113,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -220,8 +223,6 @@ protected override void Update(GameTime gameTime) // Play the collect sound effect _collectSoundEffect.Play(); } - - base.Update(gameTime); } private void AssignRandomBatVelocity() diff --git a/articles/tutorials/building_2d_games/15_audio_controller/snippets/game1.cs b/articles/tutorials/building_2d_games/15_audio_controller/snippets/game1.cs index 3841f24c..6f084b23 100644 --- a/articles/tutorials/building_2d_games/15_audio_controller/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/15_audio_controller/snippets/game1.cs @@ -107,6 +107,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -214,8 +217,6 @@ protected override void Update(GameTime gameTime) // Play the collect sound effect. Audio.PlaySoundEffect(_collectSoundEffect); } - - base.Update(gameTime); } private void AssignRandomBatVelocity() diff --git a/articles/tutorials/building_2d_games/16_working_with_spritefonts/snippets/game1.cs b/articles/tutorials/building_2d_games/16_working_with_spritefonts/snippets/game1.cs index f0583f57..0b766493 100644 --- a/articles/tutorials/building_2d_games/16_working_with_spritefonts/snippets/game1.cs +++ b/articles/tutorials/building_2d_games/16_working_with_spritefonts/snippets/game1.cs @@ -131,6 +131,9 @@ protected override void LoadContent() protected override void Update(GameTime gameTime) { + // Update the InputManger inside base.Update() right away. + base.Update(gameTime); + // Update the slime animated sprite. _slime.Update(gameTime); @@ -241,8 +244,6 @@ protected override void Update(GameTime gameTime) // Increase the player's score. _score += 100; } - - base.Update(gameTime); } private void AssignRandomBatVelocity()