Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Opening Game Center in our game


If the user is authenticated, we will add a button to the MenuScene class so they can open the leaderboard and view achievements from within our game. Alternatively, players can always use the Game Center app in iOS to view their progress.

Follow these steps to create a leaderboard button in the menu scene:

  1. Open MenuScene.swift in Xcode.

  2. Add a new import statement at the top of the file so we can use the GameKit framework:

    import GameKit
  3. Update the line that declares the MenuScene class so that our class adopts the GKGameCenterControllerDelegate protocol. This allows the Game Center screen to inform our scene when the player closes the Game Center:

    class MenuScene: SKScene, GKGameCenterControllerDelegate {
  4. We need a function that will create the leaderboard button and add it to the scene. We will call this function once the Game Center authenticates the player. Add a new function to the MenuScene class named createLeaderboardButton as shown here:

    func createLeaderboardButton...