Browse Source

Fix duplicate mob squash logic in Your first 3D game (#7767)

Jackie Xu 1 year ago
parent
commit
8625ccb362

+ 8 - 0
getting_started/first_3d_game/06.jump_and_squash.rst

@@ -255,6 +255,8 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
                     # If so, we squash it and bounce.
                     mob.squash()
                     target_velocity.y = bounce_impulse
+                    # Prevent further duplicate calls.
+                    break
 
  .. code-tab:: csharp
 
@@ -279,6 +281,8 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
                     // If so, we squash it and bounce.
                     mob.Squash();
                     _targetVelocity.Y = BounceImpulse;
+                    // Prevent further duplicate calls.
+                    break;
                 }
             }
         }
@@ -309,6 +313,10 @@ With dot products, when the result is greater than ``0``, the two vectors are at
 an angle of fewer than 90 degrees. A value higher than ``0.1`` tells us that we
 are roughly above the monster.
 
+After handling the squash and bounce logic, we terminate the loop early via the ``break`` statement
+to prevent further duplicate calls to ``mob.squash()``, which may otherwise result in unintended bugs
+such as counting the score multiple times for one kill.
+
 We are calling one undefined function, ``mob.squash()``, so we have to add it to
 the Mob class.
 

+ 4 - 0
getting_started/first_3d_game/07.killing_player.rst

@@ -395,6 +395,8 @@ Finally, the longest script, ``Player.gd``:
                     # If so, we squash it and bounce.
                     mob.squash()
                     target_velocity.y = bounce_impulse
+                    # Prevent further duplicate calls.
+                    break
 
         # Moving the Character
         velocity = target_velocity
@@ -496,6 +498,8 @@ Finally, the longest script, ``Player.gd``:
                         // If so, we squash it and bounce.
                         mob.Squash();
                         _targetVelocity.Y = BounceImpulse;
+                        // Prevent further duplicate calls.
+                        break;
                     }
                 }
             }

+ 4 - 0
getting_started/first_3d_game/09.adding_animations.rst

@@ -369,6 +369,8 @@ Here's the *Player* script.
                     # If so, we squash it and bounce.
                     mob.squash()
                     target_velocity.y = bounce_impulse
+                    # Prevent further duplicate calls.
+                    break
 
         # Moving the Character
         velocity = target_velocity
@@ -477,6 +479,8 @@ Here's the *Player* script.
                         // If so, we squash it and bounce.
                         mob.Squash();
                         _targetVelocity.Y = BounceImpulse;
+                        // Prevent further duplicate calls.
+                        break;
                     }
                 }
             }