Now Reading
AR Madness — our open source ARKit game tutorial! Part Five — Game Management

AR Madness — our open source ARKit game tutorial! Part Five — Game Management

The end is in sight! Welcome to the final part of the epic AR Madness game tutorial!

TL:DR

If you want to skip the tutorial and just get the code to run on your phone — get it here on Github.

In part five, we’ll be adding some game management features to allow the game to be started and ended etc.

Game Management logic

We now have a fun game that we can play — but we’ve no way to “end” the game, or to start a new one. So, what we’ll do now is:

  • Create a new ViewController for the “Home” screen
  • Add a button to this screen to start the game
  • Add a label to show the latest score in the game
  • Add a timer to the game screen and end the game when the timer reaches zero

So — drag out a new ViewController to the Storyboard:

Select the new ViewController and then click the “Is Initial View Controller” box in the Attributes Inspector on the right, this will make this new screen the first one seen by the player when the app starts:

Let’s change the background color, and add in a button and a label. For the button I used the “playButtonIcon” icon that’s in the assets pack — make sure it’s in the Assets.xcassets folder, if not, drag it in now.

We need to hook the button and the label up to some code — so first add a new swift file for the new ViewController.

Right-click on the “AR Madness” folder in your project explorer, and select New File:

select Cocoa Touch Class:

call it something like “HomeViewController” and make sure it’s a subclass of UIViewController:

Once created, link the new file to the new ViewController in the Storyboard:

Now we create outlets for the button and label (note you can’t just copy and paste this code, you actually need to link the code to the objects in the Storyboard):

Now — when the play button is hit by the user, we want to navigate to the game ViewController. We’ll use a Segue for this — in the Storyboard, control-drag from the HomeViewController to the ViewController:

and select Present Modally in the dialog box:

This will create a Segue on the Storyboard. Select it and give it an id of “homeToGameSegue”:

Awesome. Now back in the HomeViewController.swift file, edit the onPlayButton function as follows:

@IBAction func onPlayButton(_ sender: Any) {
performSegue(withIdentifier: "homeToGameSegue", sender: self)
}

Run the app and when you hit the play button, you should now move from the Home screen to the game.

We can now start the game, but we need a way to finish it aswell.

Ending the game

Go back to the ViewController.swift file and add this code:

What’s going on here?

var seconds = 60
var timer = Timer()
var isTimerRunning = false

creates some variables we’ll need for the timer.

func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)
}

actually creates the timer and references this function:

@objc func updateTimer() {
if seconds == 0 {
timer.invalidate()
gameOver()
}else{
seconds -= 1
timerLabel.text = "\(seconds)"
}
}

which updates the timerLabel UILabel, decrements the seconds and checks to see have we reached 0. If so, it calls the gameOver function, which we haven’t written yet!

func gameOver(){
//store the score in UserDefaults
let defaults = UserDefaults.standard
defaults.set(score, forKey: "score")
   //go back to the Home View Controller
self.dismiss(animated: true, completion: nil)
}

This stores the current score in UserDefaults (so we can use it back on the HomeViewController to show the player their score) and we then dismiss the current ViewController which navigates the player back to the HomeViewController.

Finally, we need to start the timer, so add the lines in bold to the end of viewDidLoad:

   //play background music
playBackgroundMusic()
   //start timer
runTimer()
}

Fantastic — we only have one thing left to do! After five parts!!

Go back to the HomeViewController.swift file and add this function:

override func viewWillAppear(_ animated: Bool) {
let defaults = UserDefaults.standard
if let gameScore = defaults.value(forKey: "score"){
let score = gameScore as! Int
scoreLabel.text = "Score: \(String(score))"
}
}

This gets the saved score (if it exists) from UserDefaults and displays it on screen, via the scoreLable UILabel.

Run the app and see if it works! If it does:

AND WE’RE DONE!!!!!

I hope you made it through the five parts and you have a working AR game.

Software we used:

  • Xcode — Apple’s free IDE to make iOS apps
  • Audacity — to edit our sounds files
  • Photoshop — to edit our images

Credits:

Author Bio —  Andy O’Sullivan

Innovation in Liberty IT | Creator of http://appsandbiscuits.com | Gaeilge | all content my own opinion

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.