Update on p.106, checkCollisions()

func checkCollisions() {
var hitCats: [SKSpriteNode] = []
enumerateChildNodes(withName: "cat") { node, _ in
let cat = node as! SKSpriteNode
if cat.frame.intersects(self.zombie.frame) {
hitCats.append(cat)
}
}
for cat in hitCats {
zombieHit(cat: cat)
}
var hitEnemies: [SKSpriteNode] = []
enumerateChildNodes(withName: "enemy") { node, _ in
let enemy = node as! SKSpriteNode
if node.frame.insetBy(dx: 20, dy: 20).intersects(
self.zombie.frame) {
hitEnemies.append(enemy)
}
2D Apple Games by Tutorials Chapter 3: Actions
raywenderlich.com 106
}
for enemy in hitEnemies {
zombieHit(enemy: enemy)
}
}

I just wanted to indicate that in XCode 9.1 with the checkCollision() function on page 106 and 107 [Collision Detection section], that the two references to self.zombie.frame don’t compile. Changing to just zombie.frame seems to fix the issue. Why should self be needed here?

I have zombie as a property at the beginning of the file.

Swift requires you to explicitly use the self keyword inside closures in order to make you aware that the closure itself may capture values and create reference cycles because of that. Therefore, the above code should work as it is. Please make sure that you are using the book’s latest version and update and let me know what error do you get exactly over here. Thank you! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.