Apple's newest version of its flagship programming language, Swift 4.2, is the perfect choice for game developers. As it matures, Swift is realizing its potential to be something special, a revolutionary tool for app creators. Swift is the gateway for developers to create the next big game in the Apple ecosystem. We have only started to explore the wonderful potential of mobile gaming, and Swift is the modernization we need for our toolset. Swift is fast, safe, current, and attractive to developers coming from other languages. Whether you are new to the Apple world, or a seasoned veteran of Objective-C, I think you will enjoy making games with Swift.
Apple's website states the following:
"Swift is a successor to the C and Objective-C languages."
My goal in this book is to guide you step by step through the creation of a 2D and a 3D game for iPhones and iPads. We will start with installing the necessary software, working through each layer of game development, ultimately publishing our new game to the App Store.
We will also have some fun along the way! For the 2D game, we aim to create an endless flyer game featuring a magnificent flying penguin named Pierre. What is an endless flyer? Picture hit games such as iCopter, Flappy Bird, Whale Trail, Jetpack Joyride, and many more—the list is quite long.
Endless flyer games are popular on the App Store, and the genre necessitates that we cover many reusable components of 2D game design. I will show you how to modify our mechanics to create many different game styles.
For the 3D game, we will be using SceneKit. Like SpriteKit, which is used to develop 2D games, SceneKit is a framework developed by Apple to make 3D games. The 3D game we will be an obstacle avoidance game, in which there will be obstacles players will have to either go under or jump over.
In later chapters, we will see how to make augmented reality games using Apple's newest framework, ARKit. With ARKit, you can make really stunning augmented reality games. We will see see how to develop AR games using SpriteKit and SceneKit. Along with developing a 2D AR game, we will also see how to bring the SceneKit game we developed for 3D to life using the ARKit. In addition, we will also see how to create a Multipeer Augemented Reality App using ARKit and Multipeer Connectivity framework.
My hope is that our demo project will serve as a template for your own creative tasks. Before you know it, you will be publishing your own game ideas using the techniques we explore together.
In this chapter, we will cover the following topics:
Why you will love Swift
What you will learn in this book
New in Swift 4.2
Setting up your development environment
Creating your first Swift game
Exploring the SpriteKit demo
Swift, as a modern programming language, benefits from the collective experience of the programming community; it combines the best parts of other languages and avoids poor design decisions. Here are a few of my favorite Swift features:
Beautiful syntax: Swift's syntax is modern and approachable, regardless of your existing programming experience. Apple have balanced syntax with structure to make Swift concise and readable.
Interoperability: Swift can plug directly into your existing projects and run side by side with your Objective-C code.
Strong typing: Swift is a strongly typed language. This means the compiler will catch more bugs at compile time, instead of when your users are playing your game! The compiler will expect your variables to be of a certain type (
int
,string
, and so on) and will throw a compile-time error if you try to assign a value of a different type. While this may seem rigid if you are coming from a weakly typed language, the added structure results in safer, more reliable code.Smart type inference: To make things easier, type inference will automatically detect the types of your variables and constants based upon their initial value. You do not need to explicitly declare a type for your variables. Swift is smart enough to infer variable types in most expressions.
Automatic memory management: As the Apple Swift developer guide states, "memory management just works in Swift". Swift uses a method called Automatic Reference Counting (ARC) to manage your game's memory usage. Besides a few edge cases, you can rely on Swift to safely clean up and turn off the lights.
An even playing field: One of my favorite things about Swift is how quickly the language is gaining mainstream adoption. We are all learning and growing together, and there is a tremendous opportunity to break new ground.
Open source: From version 2.2 onwards, Apple made Swift open source, curating it through the website www.swift.org, and launched a package manager with Swift 3. This is a welcome change, as it fosters greater community involvement and a larger ecosystem of third-party tools and add-ons. Eventually, we should see Swift migrate to new platforms.
I will try to make this text easy to understand for all skill levels:
I will assume you are brand new to Swift as a language
This book requires no prior game development experience, though any experience you have will help
I will assume you have a fundamental understanding of common programming concepts