[Swift 2] Day 8 - Pausing the gameplay


Post any questions, solutions, or code here for Day 8.

Game Lessons


Hi, everybody :smile:
I spent the past couple of days working on this little game :smile:
Got stuck on the other project I was working on, so there you go :smiley:
Paul, I also took your advice to finally try and publish something the store, so I tried with this and omg…isn’t that a hassle…missing assets, errors…missing icons, I did the wrong thing renaming something in the build settings and I forgot exactly what, so that took forever to figure out :smile:

Anyway, here is what I got so far.
Just a few things I’ve changed:

  • added red and blue asteroids - red slow you down, blue slow the other asteroids down, both for around 3 ses
  • added high score, that gets saved to a .plist
  • added some moving starts in 2 different layers, to simulate some movement
  • added tilt switch, that switches the control from a tap to device tilt
  • with the tilt in question : added border rect to stop the ship from leaving the screen :wink:
  • I also have a few things like: the asteroids mass is a random function based on their scale :wink: and the gravity in the physics world is a function of the current score :wink:
  • I did change one more thing, thanks to my friend who tested it for me :wink: , he was getting annoyed that during the tap control you finger covers the ship. So I made the ship be some px higher that your finger location. That actually made it so much better :wink:

Cheers !


Forgot to mention one thing. I made it so you only get points if the asteroids get off the screen in the visible area. I don’t like the player getting extra points for asteroids off the screen. In order to do that I created an invisible node at the bottom of the screen, as wide as the screen and you receive a point only if the asteroids pass through it.


hi, can someone please check my project and write and link up the lines of code to put the highscore code on thanks.
Also i would much appreciate it if you could do the constraints as I am finding it really difficult and then please send it back through a dropbox link thank you.


Can you use github instead if possible ? I would be able to look at your code easier. Otherwise it will have to wait til tonight…


Here is your project back.

I have fixed your contrains a little bit.
However you can’t transition like that between scenes.
Your back to menu button just presents the UIViewController again, then when you hit Play again it presents another one.
You never close your scenes so every time you go back to the main menu you still have the scene running in the background.
Go back to menu and hit play , do that like 5 times and you’ll see how the FPS will drop dramatically.
You either have to dismiss the current scene and present a new one or you have to programatically GO BACK to the root ViewController, not use a modal segue.

To save your high score you first need 2 functions that need to be placed before the class:

func documentsDirectory() -> String {
    let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
    return documentsFolderPath

                                                   // Get path for a file in the directory

func fileInDocumentsDirectory(filename: String) -> String {
    return documentsDirectory().stringByAppendingPathComponent(filename)

You need 2 parameters here, one for the score( you already have that one) and one for the txt file where you are going to save the highscore:

let scoreSavePath = fileInDocumentsDirectory("highScore.txt")
var score: Int = 0

Then you need 4 functions to save a Text file, to Load a Text file, to save High Score and to Load High Score:

func saveText(text: String, path: String) -> Bool {
        var error: NSError? = nil
        let status = text.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: &error)
        if !status { //status == false {
            println("Error saving file at path: \(path) with error: \(error?.localizedDescription)")
        return status
    // Load text
    func loadTextFromPath(path: String) -> String? {
        var error: NSError? = nil
        let text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: &error)
        if text == nil {
            println("Error loading text from path: \(path) error: \(error?.localizedDescription)")
        return text
    func loadHighScore() {
        if let savedHighScore = loadTextFromPath(scoreSavePath) {
            hudHighScoreLabel.text = ("High Score: \(savedHighScore)")
    func saveHighScore() {
        println("Trying to save current High Score of: \(score)")
        let currentScore = score
        if let savedHighScore = loadTextFromPath(scoreSavePath) {
            if score > savedHighScore.toInt()  {
                println("Our score of: \(score) is bigger than the saved HS of: \(savedHighScore)")
                saveText("\(score)", path: scoreSavePath )
        } else {
            saveText("\(score)", path: scoreSavePath )

Study this code and you should be able to implement the high score with ease.


@binyomin did you watch any of the iOS app course videos on Auto Layout?

That will help you understand how to setup constraints.

I’ll be doing more videos on multiple screens for the games course.