Tải bản đầy đủ (.pdf) (722 trang)

2D apple games by tutorials (3rd edition) 2017 by begbie c , berg m , briscoe m

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.99 MB, 722 trang )


2D Apple Games by Tutorials

2D Apple Games by Tutorials
Caroline Begbie, Mike Berg, Michael Briscoe, Ali Hafizji, Marin Todorov and Ray
Wenderlich
Copyright ©2017 Razeware LLC.

Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.

Notice of Liability
This book and all corresponding materials (such as source code) are provided on an “as
is” basis, without warranty of any kind, express of implied, including but not limited to
the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in the
software.

Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.

raywenderlich.com

2



2D Apple Games by Tutorials

Dedications
"For my mum, who frequently challenges and always inspires me;
also for Muffin, my patient and loyal puppy."
— Caroline Begbie
"To my wonderful wife and family, who make it possible to do what I
do."
— Mike Berg
"To my father who ignited my love of computers. To my daughters
Meghan and Brynne, and all six of my grandchildren. And to Lindsay
for all the love and support."
— Michael Briscoe
"To my wife for always being supportive and to our bun in the oven,
your parents await your arrival."
— Kauserali Hafizji (a.k.a. Ali)
"To my father."
— Marin Todorov
"To the authors, editors and leads at raywenderlich.com. Teamwork
lets you dream bigger!"
— Ray Wenderlich

raywenderlich.com

3


2D Apple Games by Tutorials

About the authors

Caroline Begbie is living the dream as an indie iOS developer,
educator and explorer. She loves the graphics and animation side of
iOS and watches Disney movies "for research."

Mike Berg is a full time game artist who is fortunate enough to work
with many different indie game developers from all over the world.
When he’s not manipulating pixel colors, he loves to eat good food,
spend time with his family, play games and be happy.

Michael Briscoe is an independent software developer with over 30
years of programming experience. Learning BASIC on a Commodore
64 way back in 1984, he’s been hooked on coding ever since. He enjoys
creating simulations and games for all Apple platforms. You can visit
his website at skyrocketsoftware.wordpress.com.
Kauserali Hafizji (a.k.a. Ali) is a freelance software developer. He is
fortunate to have worked on several large projects. He loves creating
software that people use everyday whether it’s on the web, watch,
phone or tv. A good read, cool dip in the pool and a hot cheesy meal
would be the perfect end to his weekend. You can find out more about
Ali on his website at: alihafizji.com.
Marin Todorov is a part of Realm and raywenderlich.com. He’s also
the author of books and apps. You can find out more at
www.underplot.com.

Ray Wenderlich is part of a great team — the raywenderlich.com
team, a group of over 100 developers and editors from across the
world. He and the rest of the team are passionate both about making
apps and teaching others the techniques to make them. When Ray’s
not programming, he’s probably playing video games, role playing
games, or board games.


raywenderlich.com

4


2D Apple Games by Tutorials

About the editors
Kyle Gorlick is the tech editor of this book. Kyle creates mobile apps
and games. He likes to play volleyball and watch basketball. More info
at kylegorlick.com.

Chris Belanger is the editor of this book. Chris is the Book Team
Lead and Lead Editor for raywenderlich.com. If there are words to
wrangle or a paragraph to ponder, he’s on the case. When he kicks
back, you can usually find Chris with guitar in hand, looking for the
nearest beach, or exploring the lakes and rivers in his part of the
world in a canoe.
Tammy Coron is the final pass editor of this book. Tammy is an
independent creative professional and the host of Roundabout:
Creative Chaos. She’s also the co-founder of Day Of The Indie and the
founder of Just Write Code. For more information visit
TammyCoron.com.

About the artists
Mike Berg created the artwork for most of the games in this book.
Mike is a full time game artist who is fortunate enough to work with
many different indie game developers from all over the world. When
he’s not manipulating pixel colors, he loves to eat good food, spend

time with his family, play games and be happy.
Vinnie Prabhu created all the music and sounds for the games in this
book. Vinnie is a music composer/software engineer from Northern
Virginia who has created music and sound work for concerts, plays
and video games. He’s also a staff member on OverClocked ReMix, an
online community for music and video game fans. You can find Vinnie
on Twitter as @palpablevt.

raywenderlich.com

5


2D Apple Games by Tutorials

Vicki Wenderlich created many of the illustrations in this book and
the artwork for Drop Charge. Vicki is Ray’s wife and business partner.
She is a digital artist who creates illustrations, game art and a lot of
other art or design work for the tutorials and books on
raywenderlich.com. She also runs gameartguppy.com, which is a
website where she creates free and inexpensive art assets for game
developers to use in their games. When she’s not making art, she
loves hiking, a good glass of wine and attempting to create the perfect
cheese plate.

raywenderlich.com

6



2D Apple Games by Tutorials

Table of Contents: Overview
Introduction ............................................................. 18
Section I: Getting Started ................................... 33
Chapter 1: Sprites ............................................ 34
Chapter 2: Manual Movement ......................... 58
Chapter 3: Actions ........................................... 85
Chapter 4: Scenes .......................................... 116
Chapter 5: Camera ......................................... 127
Chapter 6: Labels ........................................... 138
Section II: Physics and Nodes .......................... 154
Chapter 7: Scene Editor ................................. 155
Chapter 8: Beginning Physics ....................... 186
Chapter 9: Intermediate Physics ................... 212
Chapter 10: Advanced Physics ..................... 250
Chapter 11: Crop, Video, and Shape Nodes . 290
Section III: Tile Maps ......................................... 319
Chapter 12: Beginning Tile Maps .................. 320
Chapter 13: Intermediate Tile Maps .............. 352
Chapter 14: Saving and Loading Games ....... 381
Section IV: Juice ............................................... 413
Chapter 15: Making Drop Charge .................. 414
raywenderlich.com

7


2D Apple Games by Tutorials


Chapter 16: Particle Systems ........................ 456
Chapter 17: Juice Up Your Game .................. 485
Section V: Other Platforms ............................... 513
Chapter 18: macOS Games ........................... 514
Chapter 19: tvOS Games ............................... 537
Chapter 20: watchOS Games ........................ 552
Section VI: Advanced Topics ............................ 575
Chapter 21: Game Center Achievements ...... 576
Chapter 22: Game Center Leaderboards ...... 601
Chapter 23: ReplayKit .................................... 619
Section VII: Bonus Section ............................... 644
Chapter 24: Augmented Reality & ARKit ...... 645
Chapter 25: 2D Art for Programmers ............ 672
Conclusion ............................................................ 722

raywenderlich.com

8


2D Apple Games by Tutorials

Table of Contents: Extended
Introduction ............................................................. 18
History of this book.............................................................................. 19
About this book.................................................................................... 19
iOS game development: a history ........................................................ 21
What you need .................................................................................... 22
Who this book is for ............................................................................ 22
How to use this book ........................................................................... 23

Whatʼs ahead: an overview.................................................................. 23
Section I: Getting started .................................................................... 24
Section II: Physics and nodes .............................................................. 25
Section III: Tile Maps .......................................................................... 26
Section IV: Juice .................................................................................. 27
Section V: Other Platforms .................................................................. 28
Section VI: Advanced Topics ............................................................... 29
Section VI: Bonus chapters ................................................................. 30
Book source code and forums ............................................................. 31
Book updates ....................................................................................... 31
License ................................................................................................. 31
Acknowledgements ............................................................................. 32

Section I: Getting Started ................................... 33
Chapter 1: Sprites ................................................... 34
Getting started .................................................................................... 35
Displaying a sprite ............................................................................... 46
Challenges ........................................................................................... 55

Chapter 2: Manual Movement ................................ 58
The SpriteKit game loop...................................................................... 59
Moving the zombie .............................................................................. 61
Rotating the zombie............................................................................. 77
Challenges ........................................................................................... 79

raywenderlich.com

9



2D Apple Games by Tutorials

Chapter 3: Actions .................................................. 85
Move action ......................................................................................... 85
Sequence action ................................................................................. 88
Wait-for-duration action ...................................................................... 89
Run-block action ................................................................................. 90
Reversing actions................................................................................. 91
Repeating actions................................................................................ 94
Periodic spawning ............................................................................... 95
Remove-from-parent action ................................................................ 97
Animation action ................................................................................. 98
Stopping action.................................................................................. 100
Scale action ........................................................................................ 101
Rotate action...................................................................................... 103
Group action ...................................................................................... 104
Collision detection ............................................................................. 105
The SpriteKit game loop, round 2 ...................................................... 108
Sound action ...................................................................................... 109
Sharing actions .................................................................................. 110
Challenges .......................................................................................... 111

Chapter 4: Scenes ................................................. 116
Win and lose conditions ..................................................................... 117
Creating a new scene ......................................................................... 119
Transitioning to a scene ..................................................................... 121
Creating a custom scene initializer .................................................... 122
Background music ............................................................................. 124
Challenges ......................................................................................... 125


Chapter 5: Camera ................................................ 127
Lights, camera, action! ...................................................................... 128
A scrolling background ...................................................................... 130
An endlessly scrolling background .................................................... 132
Fixing the gameplay ........................................................................... 134
Challenges ......................................................................................... 137

raywenderlich.com

10


2D Apple Games by Tutorials

Challenge 1: Fixing the enemies ........................................................ 137

Chapter 6: Labels ................................................. 138
Built-in fonts and font families ........................................................... 139
Adding a label to Zombie Conga ....................................................... 144
Alignment modes ............................................................................... 146
Loading custom fonts ........................................................................ 148
Updating the label text ....................................................................... 151
Challenges ......................................................................................... 152

Section II: Physics and Nodes .......................... 154
Chapter 7: Scene Editor ........................................ 155
Getting started .................................................................................. 156
Getting started with the scene editor ................................................ 159
File references ................................................................................... 165
SpriteKit Visual Debugger .................................................................. 170

Animations and action references ..................................................... 172
More about the timeline ..................................................................... 175
Challenges ......................................................................................... 180

Chapter 8: Beginning Physics .............................. 186
Physics in SpriteKit ............................................................................ 187
Getting started .................................................................................. 190
Creating a SpriteKit playground ......................................................... 191
Circular bodies ................................................................................... 195
Edge loop bodies ............................................................................... 198
Rectangular bodies ............................................................................ 199
Custom-shaped bodies ..................................................................... 200
Visualizing the bodies ........................................................................ 201
Bodies with complex shapes ............................................................. 203
Properties of physics bodies............................................................. 204
Applying an impulse .......................................................................... 207
Challenges......................................................................................... 209

raywenderlich.com

11


2D Apple Games by Tutorials

Chapter 9: Intermediate Physics .......................... 212
Getting started................................................................................... 213
Custom node classes......................................................................... 213
Connecting sprites to variables ......................................................... 217
Adding physics .................................................................................. 219

Introducing SKTUtils ......................................................................... 225
Background music ............................................................................. 226
Controlling your bodies ..................................................................... 227
Finishing touches .............................................................................. 239
Challenges......................................................................................... 248

Chapter 10: Advanced Physics ............................ 250
Getting started .................................................................................. 250
Introducing Level 2 ............................................................................ 253
Loading levels.................................................................................... 255
Scene editor, round 2 ........................................................................ 256
An overview of Joints......................................................................... 261
Joints in use ...................................................................................... 264
The SpriteKit game loop, Round 4 .................................................... 270
An overview of Constraints ............................................................... 270
Creating and removing joints dynamically ......................................... 274
Compound shapes ............................................................................ 279
Level progression .............................................................................. 285
Challenges ......................................................................................... 287

Chapter 11: Crop, Video, and Shape Nodes ........ 290
Getting started................................................................................... 291
Crop nodes ........................................................................................ 292
Video nodes ...................................................................................... 298
Shape nodes ...................................................................................... 310
Challenges ......................................................................................... 317

raywenderlich.com

12



2D Apple Games by Tutorials

Section III: Tile Maps ......................................... 319
Chapter 12: Beginning Tile Maps ......................... 320
Getting started................................................................................... 321
Creating a tile set .............................................................................. 322
Creating a tile map ............................................................................ 326
Adjacency groups ............................................................................. 330
Adding the player .............................................................................. 333
Adding a camera ............................................................................... 338
Player animations .............................................................................. 343
Challenges ........................................................................................ 349

Chapter 13: Intermediate Tile Maps ..................... 352
Tile map classes ................................................................................ 353
Replacing tiles with SKNodes............................................................ 357
Physics categories ............................................................................. 361
Obstacles and physics ...................................................................... 365
Tile user data ..................................................................................... 367
Creating tile maps in code ................................................................ 368
Power-ups with tile maps................................................................... 371
Breakables ......................................................................................... 374
Challenge: Add a breakable tree ....................................................... 379

Chapter 14: Saving and Loading Games ............. 381
Head-up display ................................................................................ 382
Game timers ...................................................................................... 384
Winning the game ............................................................................. 386

Game state management .................................................................. 387
Changing levels ................................................................................. 392
Scene user data ................................................................................ 394
Opening and closing the app ............................................................ 396
Saving games with NSCoding ........................................................... 401
Loading the game ............................................................................. 406
Cleaning up ........................................................................................ 410

raywenderlich.com

13


2D Apple Games by Tutorials

Challenge ........................................................................................... 412

Section IV: Juice ............................................... 413
Chapter 15: Making Drop Charge ......................... 414
Getting started .................................................................................. 416
Building the game world in the scene editor ..................................... 417
Writing the gameplay code ............................................................... 426
Game over, man! ............................................................................... 447
Challenges ........................................................................................ 449

Chapter 16: Particle Systems ............................... 456
Getting started .................................................................................. 457
Programmatic particle systems ........................................................ 460
Visually-created particle systems ..................................................... 470
Challenges ........................................................................................ 482


Chapter 17: Juice Up Your Game ......................... 485
Getting started .................................................................................. 487
Three steps for adding juice ............................................................. 487
Music and sound effects ................................................................... 490
Frame animation ............................................................................... 496
Particle effects .................................................................................. 500
Screen effects ................................................................................... 505
Sprite effects ..................................................................................... 507
Finishing touches ............................................................................... 510
Challenge ........................................................................................... 511
Where to go from here? ..................................................................... 512

Section V: Other Platforms ............................... 513
Chapter 18: macOS Games .................................. 514
Cross-platform strategies.................................................................. 515
Creating platform targets................................................................... 517
Creating a new target ........................................................................ 518
Events ................................................................................................ 523

raywenderlich.com

14


2D Apple Games by Tutorials

Scaling ............................................................................................... 526
Going deep ........................................................................................ 531
Challenge .......................................................................................... 536


Chapter 19: tvOS Games...................................... 537
Design considerations ....................................................................... 537
Review ............................................................................................... 538
The remote ........................................................................................ 542
Finishing touches .............................................................................. 549

Chapter 20: watchOS Games .............................. 552
Designing games for your wrist ........................................................ 552
Review ............................................................................................... 553
Gesture recognizers .......................................................................... 560
Working with the Digital Crown ......................................................... 564
The accelerometer ............................................................................ 568
Haptic feedback ................................................................................ 570
Finishing touches .............................................................................. 572
Where to go from here?..................................................................... 574

Section VI: Advanced Topics ............................ 575
Chapter 21: Game Center Achievements ............ 576
Getting started .................................................................................. 576
Introducing Game Center .................................................................. 577
Configuring your app to use Game Center........................................ 578
Authenticating local players .............................................................. 586
Adding achievements ........................................................................ 591
Initializing the built-in user interface ................................................ 596
Challenges ........................................................................................ 600

Chapter 22: Game Center Leaderboards ............. 601
Getting started .................................................................................. 602
Authenticating the local player.......................................................... 602

Creating a leaderboard strategy ....................................................... 602
Configuring leaderboards in iTunes Connect ................................... 603
raywenderlich.com

15


2D Apple Games by Tutorials

Reporting scores to Game Center .................................................... 608
Displaying leaderboards .................................................................... 610
Leaderboard sets ............................................................................... 612
Security in Game Center.................................................................... 616
Challenges ......................................................................................... 618

Chapter 23: ReplayKit........................................... 619
Getting started .................................................................................. 620
Integrating ReplayKit ......................................................................... 621
Creating a recording strategy ........................................................... 622
Modifying the user interface ............................................................. 623
Checking for availability .................................................................... 629
Starting and stopping recordings ..................................................... 633
Previewing and sharing recordings ................................................... 640
ReplayKit 2 ........................................................................................ 642
Where to go from here? .................................................................... 643
Challenges ........................................................................................ 643

Section VII: Bonus Section ............................... 644
Chapter 24: Augmented Reality & ARKit ............. 645
Getting started .................................................................................. 646

Requirements .................................................................................... 647
How AR works ................................................................................... 648
Rendering the view ........................................................................... 648
World tracking with sessions ............................................................ 649
Respond to session events ................................................................ 651
The current frame, camera and anchors........................................... 652
Adding bugs to the scene ................................................................. 653
A brief introduction to 3D Math ........................................................ 654
Light estimation ................................................................................ 658
Shooting bugs ................................................................................... 659
Level design ....................................................................................... 661
Firebugs ............................................................................................ 664
Anchor collision................................................................................. 666
raywenderlich.com

16


2D Apple Games by Tutorials

Where to go from here? .................................................................... 670
Challenge ........................................................................................... 671

Chapter 25: 2D Art for Programmers ................... 672
Choose your path: Hire or DIY? ......................................................... 673
How to find and hire an artist ............................................................ 674
Paying your artist .............................................................................. 676
Getting started .................................................................................. 679
Begin with a sketch ........................................................................... 679
Getting the sketch into Illustrator ..................................................... 685

Tracing the sketch with vector lines ................................................. 689
Custom stroke widths ....................................................................... 694
Coloring your artwork ........................................................................ 697
A bit about shadow and light ............................................................. 710
Exporting PNG files ............................................................................ 717
Challenges ......................................................................................... 720

Conclusion ............................................................ 722

raywenderlich.com

17


I

Introduction

In this book, you’ll learn how to make 2D games for iOS, macOS, tvOS and even
watchOS. You’ll do this using Swift and Apple’s built-in 2D game framework: SpriteKit.
However, this raises a number of questions:
• Why SpriteKit? SpriteKit is Apple’s built-in framework for making 2D games. It’s
easy to learn, especially if you already have some Swift or iOS experience.
• Why iOS? For a game developer, there’s no better platform. The development tools
are well-designed and easy to learn. Plus, the App Store makes it incredibly simple to
distribute your game to a massive audience — and get paid for it!
• Why macOS, tvOS and watchOS? One of the great things about SpriteKit is that it
works on iOS, macOS, tvOS and watchOS. If you get your game running on iOS, it’s
incredibly easy to get it working on the other platforms too.
• Why Swift? Swift is an easy language to learn, especially if you’re new to

programming.
• Why 2D? As impressive as 3D games may be, 2D games are a lot easier to make. The
artwork is far less complicated, and programming is faster and doesn’t require as
much math. All of this allows you, as a developer, to focus on creating killer
gameplay.
If you’re a beginner, making 2D games is definitely the best way to get started.
If you’re an advanced developer, making a 2D game is still much faster than making a
3D game. Since it’s not necessarily the case that you earn more money with 3D games,
why not go for the easier win? Plus, some people prefer 2D games anyway!
So rest easy — with 2D games and SpriteKit, you’re making great choices!

raywenderlich.com

18


2D Apple Games by Tutorials

Introduction

History of this book
Four years ago, we wrote a book named iOS Games by Tutorials, covering how to make
2D games with SpriteKit. One year later, we released a second edition fully ported to
Swift, as a free update for existing customers. Since then, we renamed and released the
book two more times. This year, 2D Apple Games by Tutorials has been updated once
again.
At WWDC 2017, Apple announced a lot of cool new features to both SpriteKit and
Xcode, including ARKit, which allows us to use our iOS devices for Augmented Reality.
Yes, you read that correctly!
These changes were so significant that we decided it would be better to completely

revamp the book (again!) — we even included a new game!
Wondering what’s changed? Here are the highlights:
• Swift 4 & iOS 11: All chapters have been updated to fully support Swift 4 and iOS
11.
• ARniegeddon: NEW GAME! This is a first-person shooter. You’ll add bugs to the
view and shoot them from a distance. Firebugs, just as they were in Pest Control, are
a bit tougher to destroy. You’ll have to locate bug spray and pick it up by scooping
your phone through the bug spray canister. You’ll then be able to aim at a firebug and
take it down.

About this book
This book is something special to us. Our goal at raywenderlich.com is for this to be the
best book on 2D game programming you’ve ever read.
There are a lot of game programming books out there, and many of them are quite
good, so this might be a lofty goal. But here’s what we’ve done to try to accomplish it:
• Learn by making games: Other books teach the high-level concepts and show code
snippets, but many leave you on your own to put together a complete, functioning
game. In this book, you’ll learn by making six games in a variety of genres — games
that are actually fun. Our hope is that you can and will reuse techniques or code from
these games to make your own games.
• Learn by challenges: Every chapter in this book includes some challenges at the
end that are designed to help you practice what you’ve learned. Following a tutorial

raywenderlich.com

19


2D Apple Games by Tutorials


Introduction

is one thing, but applying it yourself is quite another. The challenges in this book
take off the training wheels and push you to solidify your knowledge by grappling
with a problem on your own. Because we’re not mean, we also provide the solutions
to our challenges. But try not to look soon, or you might spoil the fun. =]
• Focus on polish: The key to making a hit game is polish — adding loads of wellconsidered details that set your game apart. Because of this, we’ve put our money
where our mouths are and invested in a top-notch artist and sound designer to
create resources for the games in this book. We’ve also included a chapter all about
polishing your game with special effects — otherwise known as adding “Juice” —
which we think you’ll love.
• High-quality tutorials: Our site is known for its high-quality programming
tutorials, and we’ve put a lot of time and care into the tutorials in this book to make
them equally valuable, if not more so. Each chapter has been put through a rigorous
multi-stage editing process — resulting in some chapters being rewritten several
times! We’ve strived to ensure that each chapter contains great technical content
while also being fun and easy to follow.
After you finish reading this book, please let us know if you think we were successful in
meeting these goals. You can email Ray anytime at
We hope you enjoy the book, — we can’t wait to see what games you make on your own!

raywenderlich.com

20


2D Apple Games by Tutorials

Introduction


iOS game development: a history
As you’ll see, it’s easy to make games with SpriteKit — but it wasn’t always so. In the
early days of iOS, your only option was to make your game with OpenGL ES, which
(along with Metal) is the lowest-level graphics API available on the platform. OpenGL
ES is notoriously difficult to learn, and it was a big barrier to entry for many beginning
game developers.
After a while, third-party developers released some game frameworks on top of
OpenGL, the most popular of which was called Cocos2D — in fact, several of us wrote a
book on the subject! Many of the games at the top of the App Store charts were made
with Cocos2D, and many developers can say that Cocos2D was their entry point into the
world of game development.
Cocos2D was a great framework, but it wasn’t written or supported by Apple. Because of
this, there were often problems when new versions of iOS were released, or with
integrating other Apple APIs into the system.
To resolve this, with iOS 7 Apple released a new framework for making 2D games:
SpriteKit. Its API is very similar to Cocos2D, with similar types for the sprites, actions
and scenes that Cocos2D developers know and love, so fans of the older framework will
have no trouble getting up to speed. SpriteKit also has a few extra bells and whistles,
like support for playing videos, making shapes and applying special image effects.
The SpriteKit API is well-designed and easy to use, especially for beginners. Best of all,
you can use it knowing that it’s fully supported by Apple and heavily optimized to make
2D games on iOS — and now with support for macOS, tvOS and watchOS, it makes it the
clear choice for Apple 2D game development.
From here on out, if you want to make a 2D game on iOS, macOS, tvOS and watchOS,
we definitely recommend using SpriteKit rather than other game frameworks. There’s
one big exception: if you want to make a cross platform game (i.e. for Android,
Windows, etc). SpriteKit is an Apple-only API so it will be more challenging to port your
game from SpriteKit to other platforms than using other options such as Unity. If you’re
interested in learning Unity, please check out our newly released book, Unity Games by
Tutorials, which you can order here: />If you just want to make something simple for Apple platforms only, SpriteKit is the

way to go. So let’s get you up to speed with SpriteKit!

raywenderlich.com

21


2D Apple Games by Tutorials

Introduction

What you need
To follow along with the tutorials in this book, you need the following:
• A Mac running macOS Sierra 10.12.4 or later.. This is so you can install the latest
version of the required development tool: Xcode.
• Xcode 9.0 or later. Xcode is the main development tool for Apple platforms. You
need to use Xcode 9.0 or later in this book. You can download the latest version of
Xcode for free from the Apple developer site: />download/
• An iPhone or iPad running iOS 11 or later, and a paid membership to the iOS
development program [optional]. For most of the chapters in the book, you can
run your code on the iOS 11 Simulator that comes with Xcode. However, there are a
few chapters later in the book that require a device for testing. Also note that
SpriteKit performs better on physical devices than it does in the Simulator, so your
frame rates will appear lower than expected when running your game in the
Simulator.
• An Apple TV [optional]: You do not need an Apple TV since you can work with the
Apple TV simulator, but it’s definitely handy to test with a physical remote — plus
awesome to see your games on the big screen!
• An Apple Watch [optional]: Just like you do not need an Apple TV, you also do not
need an Apple Watch; using the simulator is perfectly acceptable.

If you don’t have the latest version of Xcode installed, be sure to do that before
continuing with the book.

Who this book is for
This book is for beginning to advanced iOS developers. Wherever you fall on that
spectrum, you’ll learn a lot from this book!
This book does require some basic knowledge of Swift. If you do not know Swift, you can
still follow along with the book because all of the instructions are in step-by-step
format. However, there will likely be parts that are confusing due to gaps in your
knowledge. Before beginning this book, you might want to go through our Swift
Apprentice book, which covers the basics of Swift development:
• />
raywenderlich.com

22


2D Apple Games by Tutorials

Introduction

How to use this book
There are two ways to use this book, depending on whether you are a complete
beginner to Apple game development or an advanced developer with knowledge of
other 2D game frameworks.

If you are a complete beginner
If you’re a complete beginner to Apple game development, the best way to read this
book is from cover to cover. We have arranged the chapters to introduce the material in
the most logical manner to build up your skills one layer at a time.


If you are an advanced developer
If you’re an advanced developer with knowledge of other 2D game frameworks, you’ll
have an easier time adapting to SpriteKit, as the core concepts and syntax will look very
familiar.
Our suggestion is to skim through the early chapters and focus more on the later, more
advanced chapters, or where you have a particular interest.
Don’t worry — you can jump right into any chapter in the book, because we’ll always
have a starter project waiting for you!

Whatʼs ahead: an overview
2D Apple Games by Tutorials is split into six sections, moving from beginning to
advanced topics. In each section, you’ll create a complete mini-game, from scratch! The
book also includes a bonus chapter at the end that we think you’ll enjoy.
Take a look at what’s ahead!

raywenderlich.com

23


2D Apple Games by Tutorials

Introduction

Section I: Getting started
This section covers the basics of making 2D games with SpriteKit. These are the most
important techniques, the ones you’ll use in almost every game you make. By the time
you reach the end of this section, you’ll be ready to make your own simple game.


Throughout this section, you’ll create an action game named Zombie Conga, where you
take the role of a happy-go-lucky zombie who just wants to party!
There are six chapters in this section; they are:
1. Chapter 1, Sprites: Get started by adding your first sprites to the game: the
background and the zombie.
2. Chapter 2, Manual Movement: You’ll make the zombie follow your touches
around the screen and get a crash-course in basic 2D vector math.
3. Chapter 3, Actions: You’ll add cats and crazy cat ladies to the game, as well as
basic collision detection and gameplay.
4. Chapter 4, Scenes: You’ll add a main menu to the game, as well as win and lose
scenes.
5. Chapter 5, Camera: You’ll make the game scroll from left to right, and finally, add
the conga line itself.

raywenderlich.com

24


2D Apple Games by Tutorials

Introduction

6. Chapter 6, Labels: You’ll add a label to show the zombie’s lives and the number of
cats in his conga line.

Section II: Physics and nodes
In this section, you’ll learn how to use the built-in 2D physics engine included with
SpriteKit. You’ll also learn how to use special types of nodes that allow you to play
videos and create shapes in your game.


In the process, you’ll create a physics puzzle game named Cat Nap, where you take the
role of a cat who has had a long day and just wants to go to bed.
There are five chapters in this section; they are:
7. Chapter 7, Scene Editor: You’ll begin by creating the first level of the game. By the
end, you’ll have a better understanding of Xcode’s level designer, better known as
the scene editor.
8. Chapter 8, Beginning Physics: In this chapter, you’re going to take a little detour
in order to learn the basics of creating physics simulations for your games. As a
bonus, you’ll learn how to prototype games inside an Xcode playground.
9. Chapter 9, Intermediate Physics: You’ll learn about physics-based collision
detection and create custom classes for your SpriteKit nodes.
raywenderlich.com

25


×