Thursday, June 12, 2014

HowTo: Implement a Delegate Pattern to communicate between a SpriteKit Scene and the parent ViewController

Welcome to Part 10 of my blog series about game development.


Today I'll show how to use a delegate pattern to communicate with the ViewController which contains our Scene. You can download the project from GitHub: v0.7 if you haven't completed part 8.

One of the most important characteristics of this pattern is 'Inversion of control'. The goal of this principle is to remove dependencies between objects. The main idea is that a 'delegator' object 'delegates' the execution a task to a 'delegate object'. You can find multiple definitions and tutorials about the Delegate Pattern. Therefore I'll not explain the pattern itself, but show how you can use it for the following Situation:



After creating a new SpriteKit project you typically have a ViewController which has a reference to a SKScene object:

The ViewController presents the screen and starts the game. This works fine as long as the game lasts. But what can you do, if the game is over?