lastUpdateState = currentState;
if (postUpdateStateChange != 0)
currentState = postUpdateStateChange;
The above version of the method now contains code to support animation and sound playback. In some places, additional logic was added since we now have to wait for animations to finish playing back before advancing the state of the catapult. Additionally, the method now utilizes several helper methods, which we will implement.
Note: Take the time to go over the above method as it demonstrates how to take animation times into consideration.
Create a new method called UpdateAimAccordingToShotStrength:
private void UpdateAimAccordingToShotStrength()
var aimAnimation = animations["Aim"];
int frameToDisplay =
Convert.ToInt32(aimAnimation.FrameCount * ShotStrength);
aimAnimation.FrameIndex = frameToDisplay;
This method translates the current shot strength into a frame in the catapult’s aiming animation. This will make the catapult arm stretch further as the user increases the shot power.
Create a new method called AimReachedShotStrength:
private bool AimReachedShotStrength()
return (animations["Aim"].FrameIndex ==
(Convert.ToInt32(animations["Aim"].FrameCount * ShotStrength) - 1));
The above method complements the “UpdateAimAccordingToShotStrength” method, checking whether the current aim animation frame matches the shot strength.
Create a new method called StartFiringFromLastAimPosition:
private void StartFiringFromLastAimPosition()
int startFrame = animations["Aim"].FrameCount -
Page | 86