09/19/2014

A Flappy Bird Swift Example – FlappyBowst (Part 3 of 3)

mobile

At last, after weeks of Python development I’m back for the final blog post of our Flappy Bird clone. What’s happened in the time since our last post? We’ve seen a few changes in the Xcode 6 beta and we’re now on Xcode 6 GM; Swift hit a milestone of version 1.0; the new iPhone was released to pre-order, and a million other things.

When we left off we had created everything but the final few nodes to make the game an actual game. So let’s start in adding the final few things.

Up in our variable declarations let’s add some new variables to play with.

We have a texture for the pipes, both up and down facing; an SKNode to store the pipes and some window dressings for the final touches.

Let’s add a few constants to get the rest of our variables.

We added the pipeGap and a starting message.

Before we can spawn our pipes we’ll need some actions declared, so let’s add some actions to the variable declaration.

We’ll initialize these later, for now we just want them to exist.

We need to spawn the pipes FOREVER…well relatively speaking, so we need to create a method who’s job is to spawn pipes.

The code is verbose and could be trimmed down, but that’s not the point of this blog series, so we’ll talk about the intent of the code, and you can refactor as you like. We create an SKNode to hold the pair of top and bottom pipes, set its position, create the top and bottom nodes, create our “hit box” and add them all to the pipePair node; then we add the move pipes action and add the whole thing to our pipesNode that we created earlier.
We still have a few steps before we can run it. We need to setup the pipes and add our actions. Let’s start with the pipe setup function.

Here we initialize the pipes the same way we did the sky and the ground. Call our setupPipes function in the didMoveToView function and run it.
We have pipes moving but you may have noticed that when you hit one it bounces you back. Let’s fix that. First we’ll initialize our actions.

We need to add a function to restart the game.
Let’s add the restart code to our update function.
Setup the score labels.

Add the start label setup function and call it in the didMoveToView method.
Add some code to the touchesBegan function to handle the pause on restart.
Add view.paused = true to the top of our didMoveToView.

That about wraps it up, there’s plenty that can be added to take this from demo to full game, however I’ll leave that up to you.

More Thoughts

Need help with your next digital project?

Click on that button and fill in the simple form.