Skip to content

Latest commit

 

History

History
 
 

day-056

Day 56: Project 14: Whack-a-Penguin, Part Two

Follow along at https://www.hackingwithswift.com/100/56.

📒 Field Notes

This day covers the second and final part of Project 14: Whack-a-Penguin in Hacking with Swift.

I previously created projects alongside Hacking with Swift in a separate repository, and you can find Project 14 here. Even better, though, I copied it over to Day 55's folder so I could extend it for 100 Days of Swift.

With that in mind, Day 56 focuses on several specific topics:

  • SKAction sequences
  • Wrapping up and extending the project with a set of challenges

SKAction Sequences

I alluded to it in [Day 55] when I was highlighting the way I created an SKAction group for hiding a penguin in a slot...

extension WhackSlot {

    var showAction: SKAction {
        return SKAction.moveBy(x: 0, y: 80, duration: 0.05)
    }

    var hideActions: SKAction {
        return SKAction.group([
            SKAction.moveBy(x: 0, y: -80, duration: 0.05),
            SKAction.scale(to: 0.08, duration: 0.025),
            SKAction.run { [weak self] in
                self?.isShowingPenguin = false
            },
        ])
    }
}

... but SKAction sequences allow us to take that composability even further. With SKAction.sequence, we can declare SKActions to be run one after another, and since an SKAction.group is, itself, an SKAction, we can run sequences with groups without skipping a beat:

func whack() {
    guard isShowingPenguin && !isWhacked else { return }

    isWhacked = true

    let delay = SKAction.wait(forDuration: 0.25)

    penguinNode.run(SKAction.sequence([delay, hideActions]))
}

🥅 Challenges

Challenge 1

Record your own voice saying "Game over!" and have it play when the game ends.

Challenge 2

When showing “Game Over” add an SKLabelNode showing the user's final score.

Challenge 3

Use SKEmitterNode to create a smoke-like effect when penguins are hit, and a separate mud-like effect when they go into or come out of a hole.

📸 Screenshots

🔗 Additional/Related Links