sprite-kit

Topics related to sprite-kit:

Getting started with sprite-kit

SpriteKit is a 2D Game Engine developed by Apple. It provides high level APIs and a wide range of functionalities to developers. It also contains an internal Physics Engine.

It is available on every Apple platform

  • iOS
  • macOS
  • tvOS
  • watchOS (>= 3.0)

Note: If you wish to develop using 3D graphics you need to use SceneKit instead.

The core building blocks of SpriteKit are:

  • SKView: a view in which SKScenes are presented.
  • SKScene: a 2D scene that is presented in an SKView and contains one or more SKSpriteNodes.
  • SKSpriteNode: an individual 2D image that can be animated around the scene.

Other related building blocks are:

  • SKNode: a more general node that can be used in a scene to group other nodes together for more complex behaviour.
  • SKAction: single or groups of actions that are applied to SKNodes to implement animations and other effects.
  • SKPhysicsBody - allows physics to be applied to SKNodes to allow them to behave in a realistic manner, including falling under gravity, bouncing off each other and following ballistic trajectories.

Official documentation.

SKSpriteNode (Sprites)

SKView

Detecting touch input on iOS devices

SKScene

SKScene represents a single scene in a SpriteKit application. An SKScene is 'presented' into an SKView. SKSpriteNodes are added to the scene to implement the actual sprites.

Simple applications may have a single SKScene that contains all the SpriteKit content. More complex apps may have several SKScenes that are presented at different times (e.g. an opening scene to present the game options, a second scene to implement the game itself and a third scene to present the 'Game Over' results).

Timed functions in SpriteKit: SKActions vs NSTimers

When should you use SKActions to perform timer functions? Almost always. The reason for this is because Sprite Kit operates on an update interval, and the speed of this interval can be changed throughout the life time of the process using the speed property. Scenes can also be paused as well. Since SKActions work inside the scene, when you alter these properties, there is no need to alter your time functions. If your scene is 0.5 seconds into the process, and you pause the scene, you do not need to stop any timers and retain that 0.5 second difference. It is given to you automatically, so that when you unpause, the remaining time continues.

When should you use NSTimers to perform timer functions? Whenever you have something that needs to be timed outside of the SKScene environment, and also needs to be fired even when the scene is paused, or needs to fire at a constant rate even when the scene speed changes.

This is best used when working with both UIKit controls and SpriteKit controls. Since UIKit has no idea about what goes on with SpriteKit, NSTimers will fire regardless of the state of the SKScene. An example would be we have a UILabel that receives an update every second, and it needs data from inside your SKScene.

SKAction

SKNode Collision

The determinants of Sprite Kit collision and contact event processing are the relationship settings, created by you, of categoryBitMask, collisionBitMask and contactTestBitMask for each of your interacting object types. By rationally setting these in service of your desired outcomes from contacts and collisions, you determine which types can collide and inform of contacts with others, and avoid undesired collision, contact and physics processing overhead.

For each type of 'entity' you can set all three:

  1. categoryBitMask : a category specific to this type of node
  2. collisionBitMask : a collision differentiator, can be different from above
  3. contactTestBitMask : a contact differentiator, can be different from both above

The general steps to implement collisions & contacts are:

  1. set physic body size, shape and (sometimes) mass
  2. add necessary BitMasks for your node type from category, collision and contact above
  3. set scene as a contact delegate enabling it to check and inform of collisions and contacts
  4. implement contact handlers and any other pertinent logic for physics events

UIKit elements with SpriteKit

Physics