WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Project23: Errata

Forums > Books

In project 23, for the first of three parts to create enemies, the code in the book shows:

    func createEnemy(forceBomb: ForceBomb = .random) {
        let enemy = SKSpriteNode
        enemy = SKSpriteNode()
        var enemyType = Int.random(in: 0...6)
        if forceBomb == .never {
            enemyType = 1
        } else if forceBomb == .always {
            enemyType = 0
        if enemyType == 0 {
           //bomb code here   
        } else {
            enemy = SKSpriteNode(imageNamed: "penguin")

There are two problems with the line

let enemy = SKSpriteNode

First, if you use 'let', then the the line enemy = SKSpriteNode(imageNamed: "penguin"). Second, the line as it is uses the assignment operator '=' but does not instantiate an instance of the SKSpriteNode class with '()' at the end.

The correct code when declaring that variable is:

var enemy: SKSpriteNode!

This makes the variable a 'var' that can be changed. A '!' is used to say that the variable will be defined below for sure.


Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.