How to Use Swift SpriteKit For Beginners

Learn how to use swift SpriteKit for beginners. SpriteKit is on of the easiest way to start building games on the iPhone, iPad, Macs and even the Apple Watch and TV.

What is SpriteKit?

SpriteKit is a game development engine released by Apple in 2013.

SpriteKit is best for 2D games and the best way to learn about game physics, game loops, effects, animation and event handling.

From the official Apple SpriteKit documentation

SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps. Using a rich set of animations and physics behaviors, you can quickly add life to your visual elements and gracefully transition between screens.

SpriteKit is supported on iOS, macOS, tvOS, and watchOS, and it integrates well with frameworks such as GameplayKit and SceneKit

How to Display SpriteKit Content?

To start, we would need the SKScene object which handles the scene object in SpriteKit. All displays for SpriteKit is done to the scene object’s instance.

The scene instance is the root node and acts as somewhat as a container. Nodes are either visual elements or containers of other nodes.  There are three methods to display or render a scene

  • SKView – most straightforward, a view subclass that renders a SpriteKit Scene
  • SKRenderer – allows an app to mix SpriteKit and Metal content by rendering an SKScene into a Metal command buffer. 
  • WKInterface SKScene – a visual WatchKit element that displays a SpriteKit scene

How to Use SKView?

Using SKView when SKScene is initialised, we can load a scene (.sks) file or initliaze with size and then pass that scene to be presented by the SKView instance.

skview.presentScene(scene)

Loading Scene file example:

Initialize with a size example:

Creating a new project example:

Creating a new Game Project from Xcode and choosing SpriteKit as a Game Technology will give some sample template of initializing SKScene with a .sks file and the SKView.

The SKView object has other several cool features like control timing, performance, debugging, converting and snapshotting.

How to Use Nodes in SpriteKit?

The SpriteScene SKScene is the root node in a tree hierarchy. Each node can be a visual or non visual container element that is stacked on top of each other also known as the node tree.

Base Nodes – nonvisual

  • SKNode – container and layout tool for child nodes
  • SKCameraMode – defines point of view in a scene that are visible
  • SKReferenceNode – refers to another node

Nodes that Draw – images, shapes, particles, text, video, tiles, 3D

  • SKSpriteNode – image or color
  • SKShapedNode – mathematical shape
  • SKEmitterNode – particle effects
  • SKLabelNode – draws text
  • SKVideoNode – plays video content
  • SKTileMapNode – two-dimensional array of images
  • SK3DNode – 3D SceneKit content drawn as a flattened image

Nodes for Environment Effects – environmental effects such as audio, lightning or physics characteristics

  • SKAudioNode – plays audio
  • SKLightNode – light surrounding nodes
  • SKFieldNode – applies physics effects to nearby nodes

Nodes that Modify Drawing

  • SKEffectNode – renders its children into separate buffer to apply an effect
  • SKCropNode – masks pixels by its children so only some pixels are visible
  • SKTransformNode – allows its children to rotate in 3D

Leave a Reply

Your email address will not be published. Required fields are marked *