iOS Game Development by Example - Sample Chapter
iOS Game Development by Example - Sample Chapter
$ 44.99 US
28.99 UK
P U B L I S H I N G
E x p e r i e n c e
D i s t i l l e d
Sa
m
pl
C o m m u n i t y
Samanyu Chopra
ee
Samanyu Chopra
You can know more about Daphnis Labs at www.DaphnisLabs.com. You can write a
tweet to him at @samdonly1 or find him on Facebook for any updates. You can also
reach him at samanyu@DaphnisLabs.com.
Preface
Sprite Kit is a set of tools to develop 2D games for the Apple iOS platform. Sprite Kit
provides powerful features for graphics and the animation of images having texture,
and so on. It is one of the best available game engines for iOS devices. It is very
simple and powerful, with full support provided by Apple, hence it is more reliable
and convenient than any third-party game engine available today.
The Integrated Development Environment (IDE), Xcode, provided by Apple for
app development can also be used for Sprite Kit game development. Objective-C
or Swift, either of the two programming languages can be used for Sprite Kit game
development. Sprite Kit is already being used by many developers for iOS game
development. There is a good amount of information available that focuses on Sprite
Kit set up and development. However, a structured and concise resource discussing
about the complete development process and feature set is not currently available.
This book explains the basics of Sprite Kit development and allows a beginner
to become skilled in Sprite Kit game development using the Swift programming
language with its complete set of development features. This book is a complete
guide for Sprite Kit and a perfect starting point for those wanting to set sail on the
iOS game industry.
Preface
Chapter 4, Nodes in Sprite Kit, discusses about nodes and its subclasses in detail. It also
explains the implementation of various node subclasses in the example game.
Chapter 5, Physics in Sprite Kit, talks about physics simulation in a Sprite Kit game.
It explains the types of physics bodies. Physics capabilities are applied to the example
game in this chapter.
Chapter 6, Animating Sprites, Controls, and SceneKit, covers animating nodes and
adding controls to a Sprite Kit game. These features are added in the example game.
It also talks about SceneKit.
Chapter 7, Particle Effects and Shaders, discusses about particle effects and shaders,
along with their implementation in the example game.
Chapter 8, Handling Multiple Scenes and Levels, helps us understand the need for
different levels in a game. This chapter also explains how to create multiple scenes.
Chapter 9, Performance Enhancement and Extras, discusses in detail how to improve
the performance of a Sprite Kit game, along with performance measuring using
instruments. It also explains the scoring system, sound, and player running
animation in the example game.
Chapter 10, Revisiting Our Game and More on iOS 9, discusses the various steps
involved in the development of a game, and introduces readers to the Game
Center and discusses new features that will be introduced in iOS 9.
[1]
Chapter 1
Scenes
A level or environment in a game is termed as a scene. We make scenes as per our
requirement, such as menus, levels, and so on. So, there are different scenes for
different levels and also for different menus in a game. It's like a canvas where you
position your elements.
A scene in Sprite Kit is represented by an SKScene object. A scene holds sprites and
other contents to be rendered. To switch scenes, we can use the SKTransition class.
Nodes
Nodes are fundamental building blocks for all content in a scene. The SKScene class
is a descendant of the SKNode class, so a scene is a root node. The SKNode class does
not draw anything on scene by itself; we can think of it as a base class for other node
classes. There are node subclasses as follows:
SKSpriteNode: This can be used for drawing textured sprites, playing video
content, and more
SK3DNode: This can be used for rendering a Scene Kit scene as a 2D textured
image
[3]
SKCropNode: This can be used for cropping child nodes using a mask
graphics path
SKEffectNode: This can be used for applying a core image filter to its
child node
SKLightNode: This can be used for applying lighting and shadows to a scene
Actions
An action tells a node what to do and allows you to perform different things, such as:
Rotating nodes
Scaling of nodes
Playing sound
To create a run action, first, create the action using the particular action class,
configure the properties for the created action, and call a run action by passing
action object as a parameter. When the scene processes the node, the actions of
that particular node will be executed.
[4]
Chapter 1
Particle editor
This feature was introduced in iOS 7. Particle editor is used to add special effects
in a game, like adding a mist effect in a game scene. Here, we can customize many
things, such as:
Shaders
Shaders were introduced in iOS 8. They are used to produce a variety of special
effects; they calculate rendering effects on graphic hardware with a high degree of
flexibility, for example, we have seen ripple effects in many apps/games. Wherever
a user touches the screen, a ripple effect will be produced.
In Sprite Kit, shaders are represented by the SKShaderNode class object.
It's just a type SKNode, so we can apply any property that we apply to any SKNode.
[5]
Physics
Simulating physics in Sprite Kit can be achieved by adding physics bodies to
the scenes. A physics engine has the sole purpose of moving objects around in a
simulated world. The physics bodies take the properties of objects, such as mass,
shape material, current trajectory, and so on, and calculate a new position for all
those objects.
Every object on the Sprite Kit game scene will have a physics body. A physics body
object is connected to a node on the node tree of a particular scene. The scene will
simulate the effect of forces and collisions on those particular physics bodies that are
connected to the node tree, whenever the scene computes a new frame of animation.
We can apply a particular physics property on those nodes using their particular
physics properties such as gravity, mass, force, friction, and so on.
At the start, the update function is called to where we set up the logic of the game.
After that, the scene evaluates the actions. After the actions are evaluated, we get
a callback. After that, we set up physics, if any. When the physics simulation is
finished, we get another call with didSimulatePhysics. Then, we apply constraint
and get another callback, didApplyConstraints. The last callback method is
didFinishUpdate; we get it just before frame is completed and view is ready to
render. Finally SKView renders the scene; the frame is complete and it continues
60 times per second.
[6]
Chapter 1
Setting up a project
We have discussed many things about Sprite Kit, now it's time to see a project in
action and gain some practical knowledge.
[7]
2. Instead, you can also select File | New | Project from the file menu:
[8]
Chapter 1
4. The next window asks you to customize options for your project. Fill out the
fields as shown in the following screenshot:
Game Technology: The game framework being used, like Scene Kit,
Sprite Kit, Metal, and so on
Devices: The devices you want your game to run on; iPad, iPhone,
or both
5. Press Next and Xcode will ask where to save your new project. Choose a
directory and then click on Create.
[9]
6. After saving, it should open Xcode to your brand new Hello World project,
specifically to the project properties screen. On this screen, unselect the
Portrait option under Device Orientation. This file will be automatically
saved, so you won't have to do anything further:
[ 10 ]
Chapter 1
Result
Run the default game project by pressing + R on your keyboard, or by clicking
on the little play button in the top left corner. If a simulator isn't present, Xcode will
download one for you before launching the app. The result will look as follows:
Summary
We also learned how to create a Sprite Kit project and run Hello World in it.
In the next chapter, we will be diving deeply into scenes, and also into adding scenes
to our Platformer game.
[ 11 ]
www.PacktPub.com
Stay Connected: