Features of Sprite Kit
Sprite Kit provides many features to facilitate the development of a game. These features can be used for enhancing the experience as well as performance of the game. Let's discuss them in brief.
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:
- The number of particles
- Limit of particles allowed
- The color of particles
- The size of a particle
- The life of a particle
- The location of a particle in a scene, and so on
Texture atlas generator
Texture atlas generator combines all image files into one or more large images, in order to improve performance. We will discuss this in detail in the later chapters. It is recommended to use a lesser number of images to reduce draw calls (number of images rendering on a scene).
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.
Lighting and shadows
Lighting and shadows were introduced in iOS 8. These effects are produced using the SKLightNode
class object. The SKLightNode
object can:
- Spread a lighting effect at any desirable position on the scene
- Add lighting in any sprite
- Support colors and shadows
It's just a type SKNode,
so we can apply any property that we apply to any SKNode
.
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.
The game loop
Following is a frame life cycle diagram:
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.