SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

Slice to win

We need to modify touchesMoved() to detect when users slice penguins and bombs. The code isn't complicated, but it is long, so I'm going to split it into three. First, here's the structure – place this just before the end of touchesMoved():

let nodesAtPoint = nodes(at: location)

for case let node as SKSpriteNode in nodesAtPoint {
    if node.name == "enemy" {
        // destroy penguin
    } else if node.name == "bomb" {
        // destroy bomb
    }
}

Now, let's take a look at what destroying a penguin should do. It should:

  1. Create a particle effect over the penguin.
  2. Clear its node name so that it can't be swiped repeatedly.
  3. Disable the isDynamic of its physics body so that it doesn't carry on falling.
  4. Make the penguin scale out and fade out at the same time.
  5. After making the penguin scale out and fade out, we should remove it from the scene.
  6. Add one to the player's score.
  7. Remove the enemy from our activeEnemies array.
  8. Play a sound so the player knows they hit the penguin.

Replace the // destroy penguin with this, following along with my numbered comments:

// 1
if let emitter = SKEmitterNode(fileNamed: "sliceHitEnemy") {
    emitter.position = node.position
    addChild(emitter)
}

// 2
node.name = ""

// 3
node.physicsBody?.isDynamic = false

// 4
let scaleOut = SKAction.scale(to: 0.001, duration:0.2)
let fadeOut = SKAction.fadeOut(withDuration: 0.2)
let group = SKAction.group([scaleOut, fadeOut])

// 5
let seq = SKAction.sequence([group, .removeFromParent()])
node.run(seq)

// 6
score += 1

// 7
if let index = activeEnemies.firstIndex(of: node) {
    activeEnemies.remove(at: index)
}

// 8
run(SKAction.playSoundFileNamed("whack.caf", waitForCompletion: false))

You've now seen the two ways of collecting SpriteKit actions together: groups and sequences. An action group specifies that all actions inside it should execute simultaneously, whereas an action sequence runs them all one at a time. In the code above we have a group inside a sequence, which is common.

If the player swipes a bomb by accident, they lose the game immediately. This uses much the same code as destroying a penguin, but with a few differences:

  • The node called "bomb" is the bomb image, which is inside the bomb container. So, we need to reference the node's parent when looking up our position, changing the physics body, removing the node from the scene, and removing the node from our activeEnemies array..
  • I'm going to create a different particle effect for bombs than for penguins.
  • We end by calling the (as yet unwritten) method endGame().

Replace the // destroy bomb comment with this:

guard let bombContainer = node.parent as? SKSpriteNode else { continue }

if let emitter = SKEmitterNode(fileNamed: "sliceHitBomb") {
    emitter.position = bombContainer.position
    addChild(emitter)
}

node.name = ""
bombContainer.physicsBody?.isDynamic = false

let scaleOut = SKAction.scale(to: 0.001, duration:0.2)
let fadeOut = SKAction.fadeOut(withDuration: 0.2)
let group = SKAction.group([scaleOut, fadeOut])

let seq = SKAction.sequence([group, .removeFromParent()])
bombContainer.run(seq)

if let index = activeEnemies.firstIndex(of: bombContainer) {
    activeEnemies.remove(at: index)
}

run(SKAction.playSoundFileNamed("explosion.caf", waitForCompletion: false))
endGame(triggeredByBomb: true)

Before I walk you through the endGame() method, we need to adjust the update() method a little. Right now, if a penguin or a bomb falls below -140, we remove it from the scene. We're going to modify that so that if the player misses slicing a penguin, they lose a life. We're also going to delete the node's name just in case any further checks for enemies or bombs happen – clearing the node name will avoid any problems.

In the update() method, replace this code:

if node.position.y < -140 {
    node.removeFromParent()
    activeEnemies.remove(at: index)
}

…with this:

if node.position.y < -140 {
    node.removeAllActions()

    if node.name == "enemy" {
        node.name = ""
        subtractLife()

        node.removeFromParent()
        activeEnemies.remove(at: index)
    } else if node.name == "bombContainer" {
        node.name = ""
        node.removeFromParent()
        activeEnemies.remove(at: index)
    }
}

That's mostly the same, except now we call subtractLife() when the player lets any penguins through. So, if you miss a penguin you lose one life; if you swipe a bomb, you lose all your lives. Or at least you would if our code actually compiled, which it won't: you're missing the subtractLife() and endGame() methods!

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5