iOS Game Kit (Sprite Kit)

Here we will learn iOS gamekit in swift with example and how to use game kit or sprite kit framework in iOS to create games in swift with an example using Xcode.

iOS Game Kit (Sprite Kit)

In iOS, gamekit is a framework that is used to implement games for apple devices. The Game Kit will provide features like leader board, achievements, challenges, and many more.

 

Now we will see how to implement a simple game in iOS using game technology called spritekit in swift applications and how to use the Game kit in iOS development with example.

Create iOS Gamekit App in Swift

To create a new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In the welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project.

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose a template.

 

The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having a pre-configured interface and source code files. 

 

For this iOS Gamekit example, we will use the template “Game”. To select this one, Go to the iOS section on the left side à select Application à In the main area of dialog select “Game” and then click on the next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get a window like as shown below, in this we need to mention project name and other details for our application.

 

Product Name: “GameKit”

 

The name whatever we enter in Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Game Technology: “SpriteKit”

 

The SpriteKit framework will provide new features to create high-performance, battery-efficient 2D games easily.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all Apple devices in case if you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below.

 

create new ios game kit application in xcode

 

Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below

 

Give path to save new ios application in xcode

 

After click on Create button the Xcode will create and open a new project. In our project, Main.storyboard and GameViewController.swift are the main files that we used to design the app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

 

GameViewController.swift - It contains the source code of our application and we use this file to write any code related to our game app.

 

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below. In Game development, the color of ViewController is Black when we create a game project.

 

ios game kit app storyboard file in xcode

 

Now select GameViewController.Swift file in your project that will be like as shown below.

 

ios game kit app gamecontroller.swift file in xcode

 

GameScene.Swift file basically the scene of the object. Let suppose in your game the car and airplane are the two different object. So you need to create two “Game Scene” for this because each class will handle its own object.

 

ios game kit game scene file in xcode

 

The “GameScene.sks” file is referring to the objects. In this file, you can easily drag the objects from the object library and set the time and its speed.

 

ios game kit game scene.sks file in xcode

 

Now we will run and check the output of application. To run application, select the required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios gamekit application using simulator in xcode

Output of iOS Gamekit App in Swift

Following is the result of iOS Gamekit app in swift. Now click on App Screen you will be able to see the airplane which is rotating in a circle.

 

ios game kit app example result or output

 

If we create basic ios game kit application using Xcode that will be like as shown above. We can make required modifications in iOS game kit files to develop a game based on our requirements.