iOS Game Development By Example
上QQ阅读APP看书,第一时间看更新

Elements of Sprite Kit

Now we are going to discuss some elements of Sprite Kit, which are essential for game development. A game made in Sprite Kit consists of many scenes which are made of nodes, and the functioning of a node in a scene is determined by actions.

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
  • SKVideoNode: This can be used for playing video content
  • SKLabelNode: This can be used for rendering a text string
  • SKShapeNode: This can be used for rendering shape, based on a core graphics path
  • SKEmitterNode: This can be used for creating and rendering particles
  • SKCropNode: This can be used for cropping child nodes using a mask
  • 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
  • SKFieldNode: This can be used for applying physics effects to a specific portion of the scene

Actions

An action tells a node what to do and allows you to perform different things, such as:

  • Moving nodes in any direction
  • Making any node follow a path
  • Rotating nodes
  • Scaling of nodes
  • Showing or hiding a node
  • Changing the content of a sprite node
  • Playing sound
  • Removing nodes from a scene
  • Performing action on a child's node, and so on

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.