|
| 1 | +# Day 56: _Project 14: Whack-a-Penguin_, Part Two |
| 2 | + |
| 3 | +_Follow along at https://www.hackingwithswift.com/100/56_. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## 📒 Field Notes |
| 8 | + |
| 9 | +> This day covers the second and final part of `Project 14: Whack-a-Penguin` in _[Hacking with Swift](https://www.hackingwithswift.com/read/14)_. |
| 10 | +> |
| 11 | +> I previously created projects alongside _Hacking with Swift_ in a [separate repository](https://github.com/CypherPoet/book--hacking-with-swift), and you can find Project 14 [here](https://github.com/CypherPoet/book--hacking-with-swift/tree/master/14-whack-a-penguin/Whack%20a%20Penguin). Even better, though, I copied it over to Day 55's folder so I could extend it for _100 Days of Swift_. |
| 12 | +> |
| 13 | +> With that in mind, Day 56 focuses on several specific topics: |
| 14 | +> |
| 15 | +> - SKAction sequences |
| 16 | +> - Wrapping up and extending the project with a set of challenges |
| 17 | +
|
| 18 | + |
| 19 | +### SKAction Sequences |
| 20 | + |
| 21 | +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... |
| 22 | + |
| 23 | + |
| 24 | +```swift |
| 25 | +extension WhackSlot { |
| 26 | + |
| 27 | + var showAction: SKAction { |
| 28 | + return SKAction.moveBy(x: 0, y: 80, duration: 0.05) |
| 29 | + } |
| 30 | + |
| 31 | + var hideActions: SKAction { |
| 32 | + return SKAction.group([ |
| 33 | + SKAction.moveBy(x: 0, y: -80, duration: 0.05), |
| 34 | + SKAction.scale(to: 0.08, duration: 0.025), |
| 35 | + SKAction.run { [weak self] in |
| 36 | + self?.isShowingPenguin = false |
| 37 | + }, |
| 38 | + ]) |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +... 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: |
| 44 | + |
| 45 | +```swift |
| 46 | +func whack() { |
| 47 | + guard isShowingPenguin && !isWhacked else { return } |
| 48 | + |
| 49 | + isWhacked = true |
| 50 | + |
| 51 | + let delay = SKAction.wait(forDuration: 0.25) |
| 52 | + |
| 53 | + penguinNode.run(SKAction.sequence([delay, hideActions])) |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +## 🥅 Challenges |
| 59 | + |
| 60 | + |
| 61 | +### Challenge 1 |
| 62 | + |
| 63 | +> Record your own voice saying "Game over!" and have it play when the game ends. |
| 64 | +
|
| 65 | +- 🔗 [Commit](https://github.com/CypherPoet/100-days-of-swift/commit/69b6338a9403475f1e149c018c4ff309cb3736e9) |
| 66 | + |
| 67 | + |
| 68 | +### Challenge 2 |
| 69 | + |
| 70 | +> When showing “Game Over” add an SKLabelNode showing the user's final score. |
| 71 | +
|
| 72 | +- 🔗 [Commit](https://github.com/CypherPoet/100-days-of-swift/commit/1aaf99543ae43583ed7fe77bbed884cefee0b721) |
| 73 | + |
| 74 | + |
| 75 | +### Challenge 3 |
| 76 | + |
| 77 | +> 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. |
| 78 | +
|
| 79 | +- 🔗 [Commit](https://github.com/CypherPoet/100-days-of-swift/commit/7f112b367b6e95e8ba795f1fbc710a2354a6ccfa) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## 📸 Screenshots |
| 84 | + |
| 85 | +<div style="text-align: center;"> |
| 86 | + <img src="./screenshot-1-landscape.png" width="968px;"/> |
| 87 | + <img src="./screenshot-2.png" width="968px;"/> |
| 88 | + <img src="./screenshot-3.png" width="968px;"/> |
| 89 | + <img src="./screenshot-4.png" width="968px;"/> |
| 90 | +</div> |
| 91 | + |
| 92 | + |
| 93 | +## 🔗 Additional/Related Links |
| 94 | + |
| 95 | +- [Apple Docs: SKEmitterNode](https://developer.apple.com/documentation/spritekit/skemitternode) |
| 96 | +- [Apple Docs: Optimizing Emitter Node Performance](https://developer.apple.com/documentation/spritekit/skemitternode/optimizing_emitter_node_performance) |
0 commit comments