SpriteKit Project — AbsolutelyBrickingIt — Part 12: Add game complete scene
In this article, we’re going to add a game complete scene.
Add image assets
Let’s add the game complete scene images to Assets
:
Add game complete scene
Add a new scene to the project, called GameCompleteScene
. The sprite nodes for each of the elements to display and use a coordinator to allow the GameViewController
to handle button touches:
protocol GameCompleteSceneCoordinator: AnyObject {
func gameCompleteSceneMenuTapped(_ scene: GameCompleteScene)
func gameCompleteScenePlayTapped(_ scene: GameCompleteScene)
}
class GameCompleteScene: SKScene {
weak var coordinator: GameCompleteSceneCoordinator?
private var menuNode: SKSpriteNode?
private var playNode: SKSpriteNode?
override func didMove(to view: SKView) {
let backgroundNode = SKSpriteNode(imageNamed: "background.menu")
backgroundNode.position = CGPoint(x: 0.5 * size.width, y: 0.5 * size.height)
addChild(backgroundNode)
let gameOverNode = SKSpriteNode(imageNamed: "text.game-complete")
gameOverNode.position = CGPoint(x: 0.5 * size.width, y: 0.75 * size.height)
addChild(gameOverNode)
let menuNode = SKSpriteNode(imageNamed: "button.menu")
menuNode.position = CGPoint(x: 0.5 * size.width, y: 0.32 * size.height)
addChild(menuNode)
self.menuNode = menuNode
let playNode = SKSpriteNode(imageNamed: "button.play-again")
playNode.position = CGPoint(x: 0.5 * size.width, y: 0.2 * size.height)
addChild(playNode)
self.playNode = playNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let point = touch.location(in: self)
if playNode?.contains(point) == true {
coordinator?.gameCompleteScenePlayTapped(self)
} else if menuNode?.contains(point) == true {
coordinator?.gameCompleteSceneMenuTapped(self)
}
}
}
}
Present game complete scene
Add the game complete scene to the GameViewController
and present it instead of a console message when the game has been won:
private extension GameViewController {
...
var gameCompleteScene: GameCompleteScene {
let scene = GameCompleteScene(size: sceneSize)
scene.coordinator = self
scene.scaleMode = .resizeFill
return scene
}
}
extension GameViewController: GameSceneCoordinator {
...
func gameSceneGameWon(_ scene: GameScene) {
present(scene: gameCompleteScene)
}
}
Handle button touches
We still need the coordinator implementation before the project will run. Let’s add a new extension for that:
extension GameViewController: GameCompleteSceneCoordinator {
func gameCompleteSceneMenuTapped(_ scene: GameCompleteScene) {
present(scene: menuScene)
}
func gameCompleteScenePlayTapped(_ scene: GameCompleteScene) {
present(scene: gameScene)
}
}
Now the menu scene will be presented again when “Return to Menu” is tapped and the game scene will be presented again when “Play Again” is tapped.
Run the project in the Simulator and observe the game complete screen being shown when we destroy the only brick in the game:
Code
This article corresponds to the merge commit Add game complete scene (#12)
in the GitHub repository AbsolutelyBrickingIt