NEW! Master Swift design patterns with my latest book! >>

< Previous: Follow the sequence   Next: Game over, man: SKTexture >

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 node 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
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, SKAction.removeFromParent()])
node.run(seq)

// 6
score += 1

// 7
let index = activeEnemies.index(of: node as! SKSpriteNode)!
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:

let emitter = SKEmitterNode(fileNamed: "sliceHitBomb")!
emitter.position = node.parent!.position
addChild(emitter)

node.name = ""
node.parent?.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, SKAction.removeFromParent()])

node.parent?.run(seq)

let index = activeEnemies.index(of: node.parent as! SKSpriteNode)!
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()

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

…with this:

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

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

        node.removeFromParent()

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

        if let index = activeEnemies.index(of: node) {
            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!

Test your Swift today

Think you know Swift? My book Swift Coding Challenges will put your Swift skills to the test – it's perfect for job interviews and more!

< Previous: Follow the sequence   Next: Game over, man: SKTexture >
MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>