Time for action – detecting craters
To detect the craters on the track, perform the following steps:
1. Add the
GetTrackSegment()
method toHelper Methods
region of theMarsTrack
class as follows:public int GetTrackSegment(float position) { return track[(int)(position / 30f)]; }
2. Add a new field to the
Fields
region of theMarsRunnerPlayScreen
class to flag the game as having ended as follows:bool gameEnded = false;
3. At the very beginning of the
Update()
method in theMarsRunnerPlayScreen
class, add the following lines of code to return to the main menu if the game has ended:if (gameEnded && !otherScreenHasFocus) { ScreenManager.AddScreen(new BackgroundScreen(), null); ScreenManager.AddScreen(new MainMenuScreen(), null); ExitScreen(); }
4. Still in the
Update()
method, inside theif (IsActive)
block, add the following lines of code just after the call to therover.Update()
method:if (CheckFallInPit()) { EndGame(); }
5. Add the
checkFallInPit()
method to theMarsRunnerPlayScreen...