O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game Development with Swift

Book Description

Embrace the mobile gaming revolution and bring your iPhone game ideas to life with Swift

In Detail

Apple's new programming language, Swift, is fast, safe, accessible—the perfect choice for game development! 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 for game development. Then, you will learn how to animate sprites and textures. Along the way, you will master the physics framework, add the player character and NPCs, and implement controls. Towards the end of the book, you will polish your game with fun menus, integrate with Apple Game Center for leaderboards and achievements, and then finally, learn how to 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.

What You Will Learn

  • Deliver powerful graphics, physics, and sound in your game by using SpriteKit
  • Explore drawing and animation techniques for fun and quick results
  • Read and react to various forms of player input including physical device tilt and touch recognition
  • Master Apple's IDE, Xcode, and the accompanying toolset
  • Polish your menus and HUD to make your game stand out
  • Integrate your game with Game Center so that your players can share their high scores and achievements
  • Maximize gameplay with little-known tips and strategies for fun and repeatable action
  • Publish your hard work to the App Store and enjoy as people play your games

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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.

Table of Contents

  1. Game Development with Swift
    1. Table of Contents
    2. Game Development with Swift
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Designing Games with Swift
      1. Why you will love Swift
        1. Beautiful syntax
        2. Interoperability
        3. Strong typing
        4. Smart type inference
        5. Automatic memory management
        6. An even playing field
      2. Are there any downsides to Swift?
        1. Less resources
        2. Operating system compatibility
      3. Prerequisites
      4. What you will learn in this book
        1. Embracing SpriteKit
        2. Reacting to player input
        3. Structuring your game code
        4. Building UI/menus/levels
        5. Integrating with Game Center
        6. Maximizing fun
        7. Crossing the finish line
      5. Further research
        1. Marketing and monetizing your game
        2. Making games specifically for the desktop on OSX
      6. Setting up your development environment
        1. Introducing Xcode
      7. Creating our first Swift game
        1. Navigating our project
        2. Exploring the SpriteKit Demo
        3. Examining the demo code
        4. Cleaning up
      8. Summary
    9. 2. Sprites, Camera, Actions!
      1. Sharpening our pencils
      2. Checkpoint 2- A
      3. Drawing your first sprite
        1. Building a SKSpriteNode class
        2. Adding animation to your Toolkit
          1. Sequencing multiple animations
        3. Recapping your first sprite
      4. The story on positioning
        1. Alignment with anchor points
      5. Adding textures and game art
        1. Downloading the free assets
          1. More exceptional art
        2. Drawing your first textured sprite
          1. Adding the bee image to your project
          2. Loading images with SKSpriteNode
          3. Designing for retina
            1. The ideal asset approach
            2. My solution for now
            3. Hands-on with retina in SpriteKit
      6. Organizing your assets
        1. Exploring Images.xcassets
        2. Collecting art into texture atlases
          1. Updating our bee node to use the texture atlas
          2. Iterating through texture atlas frames
      7. Putting it all together
      8. Centering the camera on a sprite
        1. Creating a new world
      9. Checkpoint 2-B
      10. Summary
    10. 3. Mix in the Physics
      1. Laying the foundation
        1. Following protocol
        2. Reinventing the bee
        3. The icy tundra
          1. Another way to add assets
          2. Adding the Ground class
          3. Tiling a texture
          4. Running wire to the ground
        4. A wild penguin appears!
        5. Renovating the GameScene class
      2. Exploring the physics system
        1. Dropping like flies
        2. Solidifying the ground
      3. Checkpoint 3-A
      4. Exploring physics simulation mechanics
      5. Bee meets bee
        1. Impulse or force?
      6. Checkpoint 3-B
      7. Summary
    11. 4. Adding Controls
      1. Retrofitting the Player class for flight
        1. The Beekeeper
        2. Updating the Player class
        3. Moving the ground
        4. Assigning a physics body to the player
          1. Creating a physics body shape from a texture
      2. Polling for device movement with Core Motion
        1. Implementing the Core Motion code
      3. Checkpoint 4-A
      4. Wiring up the sprite onTap events
        1. Implementing touchesBegan in the GameScene
        2. Larger than life
      5. Teaching our penguin to fly
        1. Listening for touches in GameScene
        2. Fine-tuning gravity
        3. Spreading your wings
      6. Improving the camera
      7. Pushing Pierre forward
        1. Tracking the player's progress
        2. Looping the ground
      8. Checkpoint 4-B
      9. Summary
    12. 5. Spawning Enemies, Coins, and Power-ups
      1. Introducing the cast
        1. Adding the power-up star
          1. Locating the art assets
          2. Adding the Star class
        2. Adding a new enemy – the mad fly
          1. Locating the enemy assets
          2. Adding the MadFly class
        3. Another terror – bats!
          1. Adding the Bat class
        4. The spooky ghost
          1. Adding the Ghost class
        5. Guarding the ground – adding the blade
          1. Adding the Blade class
        6. Adding the coins
          1. Creating the coin classes
      2. Organizing the project navigator
      3. Testing the new game objects
      4. Checkpoint 5-A
        1. Preparing for endless flight
      5. Summary
    13. 6. Generating a Never-Ending World
      1. Designing levels with the SpriteKit scene editor
        1. Separating level data from game logic
        2. Using empty nodes as placeholders
      2. Encounters in endless flying
        1. Creating our first encounter
      3. Integrating scenes into the game
      4. Checkpoint 6-A
      5. Spawning endless encounters
        1. Building more encounters
        2. Updating the EncounterManager class
          1. Storing metadata in SKSpriteNode userData property
        3. Wiring up EncounterManager in the GameScene class
      6. Spawning the star power-up at random
      7. Checkpoint 6-B
      8. Summary
    14. 7. Implementing Collision Events
      1. Learning the SpriteKit collision vocabulary
        1. Collision versus contact
        2. Physics category masks
        3. Using category masks in Swift
      2. Adding contact events to our game
        1. Setting up the physics categories
        2. Assigning categories to game objects
          1. The player
          2. The ground
          3. The star power-up
          4. Enemies
          5. Coins
        3. Preparing GameScene for contact events
          1. Viewing console output
          2. Testing our contact code
      3. Checkpoint 7-A
      4. Player health and damage
        1. Animations for damage and game over
          1. The damage animation
          2. The game over animation
      5. Collecting coins
      6. The power-up star logic
      7. Checkpoint 7-B
      8. Summary
    15. 8. Polishing to a Shine – HUD, Parallax Backgrounds, Particles, and More
      1. Adding a heads-up display
      2. Parallax background layers
        1. Adding the background assets
        2. Implementing a background class
        3. Wiring up backgrounds in the GameScene class
      3. Checkpoint 8-A
      4. Harnessing SpriteKit's particle system
        1. Adding the circle particle asset
        2. Creating a SpriteKit Particle File
        3. Configuring the path particle settings
        4. Adding the particle emitter to the game
      5. Granting safety as the game starts
      6. Checkpoint 8-B
      7. Summary
    16. 9. Adding Menus and Sounds
      1. Building the main menu
        1. Creating the menu scene and menu nodes
        2. Launching the main menu when the game starts
        3. Wiring up the START GAME button
      2. Adding the restart game menu
        1. Extending the HUD
        2. Wiring up GameScene for game over
        3. Informing the GameScene class when the player dies
        4. Implementing touch events for the restart menu
      3. Checkpoint 9-A
      4. Adding music and sound
        1. Adding the sound assets to the game
        2. Playing background music
        3. Playing sound effects
          1. Adding the coin sound effect to the Coin class
          2. Adding the power-up and hurt sound effects to the Player class
          3. Playing a sound when the game starts
      5. Checkpoint 9-B
      6. Summary
    17. 10. Integrating with Game Center
      1. Registering an app with iTunes Connect
        1. Configuring Game Center
        2. Creating a test user
      2. Authenticating the player's Game Center account
      3. Opening Game Center in our game
      4. Checkpoint 10-A
      5. Adding a leaderboard of high scores
        1. Creating a new leaderboard in iTunes Connect
        2. Updating the leaderboard from the code
      6. Adding an achievement
        1. Creating a new achievement in iTunes Connect
        2. Updating achievements from the code
      7. Checkpoint 10-B
      8. Summary
    18. 11. Ship It! Preparing for the App Store and Publication
      1. Finalizing assets
        1. Adding app icons
        2. Designing the launch screen
        3. Taking screenshots for each supported device
      2. Finalizing iTunes Connect information
        1. Configuring pricing
      3. Uploading our project from Xcode
      4. Submitting for review in iTunes Connect
      5. Summary
    19. Index