31,19 €
Swift is the perfect choice for game development. Developers are intrigued by Swift 3.0 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.
This book starts by introducing SpriteKit and Swift's new features that can be used for game development. After setting up your first Swift project, you will build your first custom class, learn how to draw and animate your game, and add physics simulations.
Then, you will add the player character, NPCs, and powerups. To make your game more fun and engaging, you will learn how to set up scenes and backgrounds, build fun menus, and integrate with Apple Game Center to add leaderboards and achievements. You will then make your game stand out by adding animations when game objects collide, and incorporate proven techniques such as the advanced particle system and graphics. Finally, you will explore the various options available to start down the path towards monetization and publish your finished games to the App Store.
By the end of this book, you will be able to create your own iOS games using Swift and SpriteKit.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 246
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2015
Second edition: February 2017
Production reference: 1310117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B32PB, UK.
ISBN 978-1-78712-775-3
www.packtpub.com
Author
Stephen Haney
Copy Editor
Safis Editing
Reviewer
Giordano Scalzo
Project Coordinator
Ulhas Kambali
Commissioning Editor
Ashwin Nair
Proofreader
Safis Editing
Acquisition Editor
Reshma Raman
Indexer
Rekha Nair
Content Development Editor
Onkar Wani
Graphics
Abhinash Sahu
Technical Editor
Rashil Shah
Production Coordinator
Nilesh Mohite
Stephen Haney has written two books on iOS game development. He began his programming journey at the age of 8 years on a dusty, ancient laptop using BASIC. He has been fascinated with building software and games ever since. Now well versed in multiple languages, he enjoys programming as a creative outlet the most. He believes that indie game development is an art form-- an amazing combination of visual, auditory, and psychological challenges-rewarding to both the player and the creator.
He enjoyed writing this book and sincerely hopes that it directly furthers your career or hobby.
Thank you to my wonderful father, mother, and sister for their patience as I write and work, and also to my friends: Geoff, Robert, Justin, Jessie, Devin, Jason, Ben, Chris, Anna, and Shane, among others. I love you all!
Giordano Scalzo is a developer with 20 years of programming experience since the days of the ZX Spectrum.
He has worked in C++, Java, .NET, Ruby, Python, and in a ton of other languages he has forgotten the names of. After years of backend development, over the past five years, Giordano has done extensive development for iOS, releasing more than 20 apps that he wrote for clients and enterprise applications on his own.
Currently, he is a contractor in London where, through his company-Effective Code Ltd (http://effectivecode.co.uk)-he delivers code for iOS, aiming at quality and reliability.
In his spare time, when he is not crafting retro game clones for iOS, he writes and shares his thoughts on http://giordanoscalzo.com.
I’d like to thank my better half, Valentina, who lovingly supports me in everything I do: without you, none of this would have been possible. Thanks to my bright future, Mattia and Luca, for giving me lots of smiles and hugs when I needed them. Finally, my gratitude goes to my mum and my dad, who encouraged my curiosity and supported me to follow my passions, which began one day when they bought me a ZX Spectrum.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously-that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].
There has never been a better time to be a game developer. The App Store provides a unique opportunity to distribute your ideas to a massive audience. Also, now Swift 3 has arrived! Apple's Swift language is maturing and hitting its stride in version 3. Whether you are new to game development or looking to add to your expertise, I think you will enjoy making games with Swift.
With this book, my goal is to share a fundamental knowledge of Swift and SpriteKit. We will work through a complete example game so that you learn every step of the Swift development process. Once you finish this text, you will be comfortable designing and publishing your own game ideas to the App Store from start to finish.
Please reach out to me for any questions and share your game creations:
E-mail: [email protected]
Twitter: @sdothaney
The first chapter explores some of Swift’s best features. Let’s get started!
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 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, Choosing a Monetization Strategy, outlines the strategies available to indie developers who want to make money from their games.
Chapter 12, Integrating with Game Center, links our example game to the Apple Game Center for leaderboards, achievements, and friendly challenges.
Chapter 13, Ship It! Preparing for the App Store and Publication, covers the essentials of packaging your game and submitting it to the App Store.
This book uses the Xcode IDE, version 8.2.1 (Swift 3). 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 the Game Center and submit your games to the App Store.
If you wish to create and publish fun iOS games using Swift, this book is for you. You should be familiar with basic programming concepts. However, no prior game development or Apple ecosystem experience is required.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "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."
A block of code is set as follows:
import SpriteKit class GameScene: SKScene { override func didMove(to view: SKView) { } }New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Launch Xcode and navigate to File | New | Project."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/-Swift-3-Game-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Swift3GameDevelopmentSecondEdition_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/supportand enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Apple's newest version of its flagship programming language, Swift 3, is the perfect choice for game developers. As it matures, Swift is realizing its opportunity 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 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! We aim to create an endless flyer game featuring a magnificent flying penguin named Pierre. What is an endless flyer? Picture hit games like 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. My hope is that our demo project will serve as a template for your own creative works. 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:
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:
I will try to make this text easy to understand for all skill levels:
By the end of this book, you will be capable of creating and publishing your own iOS games. You will know how to combine the techniques we learned to create your own style of game and you will be well prepared to dive into more advanced topics with a solid foundation in 2D game design.
SpriteKit is Apple's 2D game development framework and your main tool for iOS game design. SpriteKit will handle the mechanics of our graphics rendering, physics, and sound playback. As far as game development frameworks go, SpriteKit is a terrific choice. It is built and supported by Apple and thus integrates perfectly with Xcode and iOS. You will learn to be highly proficient with SpriteKit as we will be using it exclusively in our demo game.
We will learn to use SpriteKit to power the mechanics of our game in the following ways:
The control schemes in mobile games must be inventive. Mobile hardware forces us to simulate traditional controller inputs, such as directional pads and multiple buttons, on the screen. This takes up valuable visible area and provides less precision and feedback than with physical devices. Many games operate with only a single input method: a single tap anywhere on the screen. We will learn how to make the best of mobile input and explore new forms of control by sensing device motion and tilt.
It is important to write well-structured code that is easy to re-use and modify as your game design inevitably changes. You will often find mechanical improvements as you develop and test your games and you will thank yourself for a clean working environment. Though there are many ways to approach this topic, we will explore some best practices to build an organized system with classes, protocols, inheritance, and composition.
We will learn to switch between scenes in our game with a menu screen. We will cover the basics of user experience design and menu layout as we build our demo game.
Game Center is Apple's built-in social gaming network. Your game can tie into Game Center to store and share high scores and achievements. We will learn how to register for Game Center, tie it into our code, and create a fun achievement system.
If you are like me, you will have dozens of ideas for games floating around your head. Ideas come easily, but designing fun gameplay is difficult! It is common to find that your ideas need gameplay enhancements once you see your design in action. We will look at how to avoid dead-ends and see your project through to the finish line. Plus, I will share my tips and tricks to ensure your game will bring joy to your players.
Creating a game is an experience you will treasure. Sharing your hard work will only sweeten the satisfaction. Once our game is polished and ready for public consumption, we will navigate the App Store submission process together. You will end up feeling confident in your ability to create games with Swift and bring them to market in the App Store.
Game development is a fun and rewarding process, even without compensation, but the potential exists to start a career, or side job, selling games on the App Store. Successfully promoting and marketing your game is an important task. I will outline your options and start you down the path to monetization.
The biggest feature in Swift 3 is syntax compatibility and stability. Apple is trying to refine its young, shifting language into its final foundational shape. Each successive update of Swift has introduced breaking syntax changes that made older code incompatible with the newest version of Swift; this is very inconvenient for developers. Going forward, Swift 3 aims to reach maturity and maintain source compatibility with future releases of the language. Swift 3 also features the following:
Swift has already made tremendous steps forward as a powerful, young language. Now Apple is working on polishing Swift into a mature, production-ready tool. The overall developer experience improves with Swift 3.