Many games use a game loop. This calls an Update() method, which takes an input and calculates the state of the game. UrhoSharp is no exception. The base class of our game has a virtual OnUpdate() method that we can override so that we can write code that will be executed with each frame. This method is called frequently, usually about 50 times per second.
We will now override the Update() method to add game logic that adds a new box every other second. Let's set this up by going through the following steps:
- In the WhackABox project, open the Game.cs file.
- Add the newBoxTtl field and the newBoxIntervalInSeconds field to the class at the beginning of the code as shown.
- Add the OnUpdate() method anywhere in the class, as shown in the following code block:
private float newBoxTtl;
private readonly float newBoxIntervalInSeconds = 2;
protected override void OnUpdate(float timeStep)
{
base.OnUpdate(timeStep);
newBoxTtl -= timeStep;
if (newBoxTtl < 0)
{
...