32,39 €
If you wish to create and publish fun iOS games using Swift, then this book is for you. You should be familiar with basic programming concepts. However, no prior game development or Apple ecosystem experience is required.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 210
Veröffentlichungsjahr: 2015
Copyright © 2015 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
Production reference: 1170715
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78355-053-1
www.packtpub.com
Author
Stephen Haney
Reviewers
Antonio Bello
Vladimir Pouzanov
Kevin Smith
Anil Varghese
Commissioning Editor
Edward Bowkett
Acquisition Editor
Reshma Raman
Content Development Editors
Prachi Bisht
Mamata Walkar
Technical Editor
Saurabh Malhotra
Copy Editors
Janbal Dharmaraj
Kevin McGowan
Rashmi Sawant
Project Coordinator
Sanjeet Rao
Proofreader
Safis Editing
Indexer
Hemangini Bari
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
Stephen Haney began his programming journey at the age of 8 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 most enjoys programming as a creative outlet. 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 beautiful girlfriend, Kayla, for her patience and advice.
Antonio Bello is a veteran software developer, who started writing code when memory was measured in bytes instead of gigabytes and storage was an optional add-on. Over his professional career, he has worked with several languages and technologies, in many cases, following a "learning by using" approach.
Today, he loves developing iOS Apps and their respective backends, favoring Swift over Objective C but loving both languages.
Vladimir Pouzanov is a systems engineer and an embedded enthusiast. He has spent countless hours hacking different mobile hardware, porting Linux to various devices on which it was not supposed to be run, and toying outside the iOS sandbox. He has been a professional iOS consultant and has been developing applications based on iOS since the first Apple iPhones were available. Later on, he switched his professional interest to systems engineering and cloud computing, but he still keeps a close eye on the mobile and embedded world.
I'd like to thank my wife for her amazing support while I was working on the review, sharing my attention between her, our daughter, and the book.
Kevin Smith is a founder and mobile developer. He released his first iPhone App in 2009. After the success of his first few apps, he founded App Press to help others build mobile apps. Through App Press, he has worked on and released countless award-winning iOS and Android apps.
Anil Varghese is a software engineer from Kerala, India, with extensive experience in iOS application development. He constantly strives to learn new technologies and better and faster ways of solving problems. He always finds time to help his fellow programmers and is an active member of developer communities, such as Stack Overflow.
You can reach him at <[email protected]> and http://anilvarghese.strikingly.com.
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://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and readPackt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
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. Now, Swift has arrived to bolster our toolkit and provide a smoother development experience. Swift is new, but is already hailed as an excellent, well-designed language. Whether you are new to game development or looking to add to your expertise, I think you will enjoy making games with Swift.
My goal in writing this book 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 with 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 best features on Swift, helps you set up your development environment, and launches your first SpriteKit project.
Chapter 2, Sprites, Camera, Actions!, 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 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 will 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 features that make every great game shine. 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, Integrating with Game Center, links our example game to the Apple Game Center for leaderboards, achievements, and friendly challenges.
Chapter 11, 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 6.3.2 (Swift 1.2). If you use a different version of Xcode, you will likely encounter syntax differences; Apple is constantly upgrading Swift's syntax.
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 to submit your games to the App Store.
If you wish to create and publish fun iOS games using Swift, then this book is for you. You should be familiar with basic programming concepts such as classes, types, and functions. However, no prior game development or Apple ecosystem experience is required. Additionally, experienced game programmers will find this book useful as they transition into game development with Swift.
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 from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.
Additionally, each chapter provides checkpoint links you can use to download the example project to that point.
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 http://www.packtpub.com/sites/default/files/downloads/0531OT_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/support and 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 new language has arrived at the perfect time for game developers. Swifthas the unique chance to be something special; a revolutionary tool for app creators. Swift is the gateway for developers to create the next big game on 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, "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, work through each layer of game development, and ultimately publish 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.
The topics in this chapter include:
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.
Swift's syntax is modern and approachable, regardless of your existing programming experience. Apple balanced syntax with structure to make Swift concise and readable.
Swift can plug directly into your existing projects and run side-by-side with your Objective-C code.
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.
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.
As the Apple Swift developer guide states, "memory management just works in Swift." Swift uses a method called Automatic Reference Counting (you will see it referred to as 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.
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.
Swift is a very enjoyable language, but we should consider these two issues when starting a new project.
Given Swift's age, it is certainly more difficult to find answers to common questions through Internet searches. Objective-C has many years' worth of discussion and answers on helpful forums like Stack Overflow. This issue improves every day as the Swift community continues to develop.
Swift projects will run on iOS7 and higher, and OSX 10.9 and higher. Swift is the wrong choice if, in a rare case, you need to target a device running an older operating system.
I will strive to make this text easy to comprehend 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 learn 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 – we will use it exclusively in our demo game.
We will learn to use SpriteKit to power the mechanics of our game:
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.