Spawning Pipes
In this section we're going to:
- Spawn a pair up pipes after we generate a new spawn position
- Scroll the pipes across the screen
- Remove pipes that are no long visible on screen
Pipe Manager Config
Let's store a new config value for our pipe scroll speed:
Spawning Pipes
Update the pipe manager class to spawn a new pair of pipes after a certain amount of time has passed:
We're storing the spriteSheet
to pass along to the pipe instances. We're tracking our pipes in a pipes
array, but spawing the pipes is pretty much exactly the same as before.
Update the pipe manager instance in main.ts
to take the spriteSheet
:
Scrolling Pipes
Nothing is happening on the canvas yet. We need to loop over every pipe now and change their position, then draw them.
Perfect! Check out the canvas and you'll see our pipes scrolling across the screen.
There is one little snag though.
Cleaning Up Pipes
Our pipes
array is constantly growing. This isn't necessarily a big deal in a game like Flappy Bird that has relatively short play sessions, but we'll be good citizens and clean up after ourselves nonetheless. If you like, you could add a console.log(this.pipes.length)
call after we push new pipes in the update()
method to see how many pipes are currently in the array. Just remember to remove it later.
Let's modify the update()
method to tidy up when we can:
We're checking to see if the x
position of any pipe is ever less than the width of the pipe (it's scrolled off screen). If it is, add it to the count, and remove it in the end. We're using shift()
to remove and modify the array in place. shift()
also removes elements from the front of the array, which is important because those will be the pipes already off screen.