Book Image

Swift Game Development - Third Edition

By : Siddharth Shekar, Stephen Haney
Book Image

Swift Game Development - Third Edition

By: Siddharth Shekar, Stephen Haney

Overview of this book

Swift is the perfect choice for game development. Developers are intrigued by Swift and want to make use of new features to develop their best games yet. Packed with best practices and easy-to-use examples, this book leads you step by step through the development of your first Swift game. The book starts by introducing Swift's best features – including its new ones for game development. Using SpriteKit, you will learn how to animate sprites and textures. Along the way, you will master physics, animations, and collision effects and how to build the UI aspects of a game. You will then work on creating a 3D game using the SceneKit framework. Further, we will look at how to add monetization and integrate Game Center. With iOS 12, we see the introduction of ARKit 2.0. This new version allows us to integrate shared experiences such as multiplayer augmented reality and persistent AR that is tied to a specific location so that the same information can be replicated on all connected devices. In the next section, we will dive into creating Augmented Reality games using SpriteKit and SceneKit. Then, finally, we will see how to create a Multipeer AR project to connect two devices, and send and receive data back and forth between those devices in real time. By the end of this book, you will be able to create your own iOS games using Swift and publish them on the iOS App Store.
Table of Contents (22 chapters)
Swift Game Development Third Edition
Contributors
Preface
Other Books You May Enjoy
Index

Preface

Swift is the perfect choice for game development. Developers are intrigued by Swift and want to make use of new features to develop their best games yet. Packed with best practices and easy-to-use examples, this book leads you step-by-step through the development of your first Swift game.

The book starts by introducing Swift's newest and best features for game development. Using SpriteKit, you will learn how to animate sprites and textures. Along the way, you will master the physics, animations, collision effects and required to build the UI aspects of the game.

You will then work on creating a 3D game using the SceneKit framework. You will see how to add monetization and integrate Game Center. Then you will dive into creating augmented reality (AR) games using SpriteKit and SceneKit.

Finally, you will see how to create a Multipeer AR project to connect two devices and send and receive data back and forth between the devices in real time.

By the end of this book, you will be able to create your own iOS games using Swift and publish them on the iOS App Store.

Who this book is for

The book is targeted at new and intermediate developers who want to update their knowledge about the changes in the new version of Swift and want to learn about the new ARKit framework for making AR games for iOS using Xcode 10.

What this book covers

Chapter 1, Designing Games with Swift, introduces you to the best features of Swift, outlines what is new in Swift 3, helps you set up your development environment, and launches your first SpriteKit project.

Chapter 2, Sprites, Camera, Action!, teaches you the basics of drawing and animating with Swift. You will draw sprites, import textures into your project, and center the camera on the main character.

Chapter 3, Mix in the Physics, covers the physics simulation fundamentals: physics bodies, impulses, forces, gravity, collisions, and more.

Chapter 4, Adding Controls, explores the various methods of mobile game controls: device tilt and touch input. We will also improve the camera and core gameplay of our example game.

Chapter 5, Spawning Enemies, Coins, and Power-ups, introduces the cast of characters we use in our example game and shows you how to create custom classes for each NPC type.

Chapter 6, Generating a Never-Ending World, explores the SpriteKit scene editor, builds encounters for the example game, and creates a system to loop encounters endlessly.

Chapter 7, Implementing Collision Events, delves into advanced physics simulation topics and adds custom events when sprites collide.

Chapter 8, Polishing to a Shine – HUD, Parallax Backgrounds, Particles, and More, adds the extra fun that makes every game shine. In this chapter, you will learn how to create parallax backgrounds, learn about SpriteKit's particle emitters, and add a heads-up display overlay to your games.

Chapter 9, Adding Menus and Sounds, builds a basic menu system and illustrates two methods of playing sounds in your games.

Chapter 10, Standing Out in the Crowd with Advanced Features, shows you how to combine the techniques you have learned to build advanced gameplay systems.

Chapter 11, Introduction to SceneKit, explains how to create a basic 3D game using the SceneKit framework.

Chapter 12, Choosing a Monetization Strategy, outlines the strategies available to indie developers who want to make money from their games.

Chapter 13 , Integrating with Game Center, links our example game to the Apple Game Center for leaderboards, achievements, and friendly challenges.

Chapter 14 , Introduction to SpriteKit in ArKit, gives an introduction to how to make an AR game using Arkit and SpriteKit.

Chapter 15 , Introduction to SceneKit in ArKit, demonstrates how to make a 3D augmented reality game using SceneKit and Arkit.

Chapter 16, Ship It! Preparing for the App Store and Publication, covers the essentials of packaging your game and submitting it to the App Store.

Chapter 17, Multipeer Augmented Reality, shows you how to create a multiplayer AR project to connect two devices in real time so they can send and receive data.

To get the most out of this book

This book uses the Xcode IDE version 10 (Swift 4.2). If you use a different version of Xcode, you will likely encounter syntax differences; Apple is constantly upgrading Swift's syntax. You can use Xcode's Edit | Convert | To Current Swift Syntax to update the code examples in this book to a newer version of Xcode.

Visit https://developer.apple.com/xcode/to download Xcode.

You will need an Apple developer account to integrate your apps with Game Center and submit your games to the App Store.

To run ARKit games you will need an iPhone SE or higher, a fifth-generation iPad, or an iPad Pro. To run the Multipeer AR project you will need two devices because it can't be tested on an emulator.

Download the example code files

You can download the example code files for this book from your account at http://www.packt.com. If you purchased this book elsewhere, you can visit http://www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at http://www.packt.com.

  2. Select the SUPPORT tab.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box and follow the on-screen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Swift-Game-Development-Third-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example; "Locate the Enemies folder in the downloadable asset bundle."

A block of code is set as follows:

import SpriteKit

class Coin: SKSpriteNode, GameSprite { 
var initialSize = CGSize(width: 26, height: 26) 
var textureAtlas: SKTextureAtlas = 
SKTextureAtlas(named: "Environment") 
var value = 1

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

import SpriteKit
import ARKit

class Scene: SKScene {
    
   
    var crosshair: SKSpriteNode!
    let scoreText = SKLabelNode(text: "00")

Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this. For example: "Select System info from the Administration panel."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit, http://www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.