Determine Next Spawn Point
Let's determine the next spawn point based on a set interval. We're going to update the spawn position every 1.5 seconds.
Pipe Manager Config
We need to store the spawn interval in seconds in our config:
Pipe Manager Timer
We'll update the pipe manager to track time, and update the spawn position every time our timer exceeds in the spawn interval:
We store our spawn interval in a class property called spawnTimerThreshold
. Within update()
we constantly add delta
to our spawnTimerAccumulator
and check if our accumulator exceeds the threshold. If so, we'll reset the accumulator and update the spawn position.
We need to replace that comment with some code to generate a random y
value between our minimum and maximum y threshold.
Random Between Helper
Let's create a helper function to generate a random number between two numbers: min
and max
. The max
value will be inclusive.
Create a new file:
Generate Next Spawn Position
Now replace the comment in update()
with some code to generate a random y
value between our minimum and maximum y threshold:
Finally, don't forget to call the update()
method on the pipe manager every frame:
Back in the canvas you should see the red line update to our new spawn position every time our spawn interval is exceeded.
We're so close. Next we'll add the pipes to the game.