In the first article of this series we set up the project, added the assets and made the logo respond to touch events. Now it’s time to refactor the hero and make the ground and skyline scroll by.
If you haven’t read the first article, I suggest you check it out at: https://www.bowst.com/uncategorized/flappy-bird-swift-example-flappybowst/
We’ll start by cleaning up the previous code and adding the ground. By the time we’re done the logo will be able to flap and fall on the ground. I’ll wait until you’re done applauding…welcome back!
Add the bit masks in our constants’ section.
If you’re using the code from our part one detour, it’s time to refactor the hero out of the “didMoveToView” method and into it’s own setup method.
Your “didMoveToView” method should now look like:
Now we’ll add a new function “setupHero” that returns a SKSpriteNode.
Let’s set the hero instance variable to the results from this method in our “didMoveToView” method.
The end result is we’re roughly back to where we were, and thus concludes our detour. Now that we’re back on the main road we can start in with adding the ground.
Add in a couple SKNodes for the scroller and for the ground itself, just under the hero instance variable.
Create a method for “setupGround” that returns nothing.
This will look similar to the hero setup, except we’re adding the some sprites to the scrollNode. This node is used to hold any of the items that scroll. The SKAction’s allow this to scroll in a very simple way.
Build the project now to see if there are any errors. If everything was entered correctly you should see the logo fall onto the scrolling ground.
Look at us go! With a similar code block we’re going to have the skyline moving in just a few moments of typing.
Call this method from “didMoveToView” so the ground is setup.
And when you run it we now have a scrolling ground and skyline. We’re almost at the point where we can yell and swear at the computer for being as frustrating as we all remember Flappy Bird to be.
In the next post of the series we’ll add in the pipes, the collision / contact detection, the score and a couple finishing touches. Hopefully by this point you’re getting a feel for the Swift syntax and seeing the potential for rapid app development.
Until then, Happy Coding!