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

Swift Game Development - Third Edition

Book Description

Embrace the mobile gaming revolution by creating popular iOS games with Swift 4.2

Key Features

  • Learn to create games for iPhone and iPad with the latest Swift Programming language
  • Understand the fundamental concepts of game development like game physics, camera action, sprites, controls, among others
  • Build Augmented reality games using ARKit for true performance

Book Description

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.

What you will learn

  • Deliver powerful graphics, physics, and sound in your game by using SpriteKit and SceneKit
  • Set up a scene using the new capabilities of the scene editor and custom classes
  • Maximize gameplay with little-known tips and strategies for fun, repeatable action
  • Make use of animations, graphics, and particles to polish your game
  • Understand the current mobile monetization landscape
  • Integrate your game with Game Center
  • Develop 2D and 3D Augmented Reality games using Apple's new ARKit framework
  • Publish your game to the App Store

Who this book is for

If you wish to create and publish iOS games using Swift, then this book is for you. No prior game development or experience with Apple ecosystem is needed.

Table of Contents

  1. Swift Game Development Third Edition
    1. Table of Contents
    2. Swift Game Development Third Edition
      1. Why subscribe?
      2. Packt.com
    3. Contributors
      1. About the authors
      2. About the reviewer
      3. Packt is Searching for Authors Like You
    4. Preface
      1. Who this book is for
      2. What this book covers
      3. To get the most out of this book
        1. Download the example code files
        2. Conventions used
      4. Get in touch
        1. Reviews
    5. 1. Designing Games with Swift
      1. Why you will love Swift
      2. Prerequisites
      3. 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
        8. Monetizing your work
      4. New in Swift 4.2
      5. Setting up your development environment
        1. Introducing and installing Xcode
        2. Creating our first Swift game
        3. Navigating our project
      6. Exploring the SpriteKit demo
      7. Examining the demo code
      8. Cleaning up
      9. Summary
    6. 2. Sprites, Camera, Action!
      1. Preparing your project
      2. Drawing your first sprite
      3. Building a SKSpriteNode class
      4. Animation—movement, scaling, and rotation
        1. Sequencing multiple animations
        2. Recapping your first sprite
      5. The story on positioning
        1. Alignment with anchor points
      6. Working with textures
        1. Downloading the free assets
        2. More exceptional art assets
        3. Drawing your first textured sprite
        4. Adding the bee image to your project
        5. Loading images with SKSpriteNode
      7. Designing for Retina
        1. The ideal asset approach
        2. Hands-on with retina in SpriteKit
      8. Organizing art into texture atlases
        1. Exploring Assets.xcassets
        2. Collecting art into texture atlases
        3. Updating our bee node to use the texture atlas
        4. Iterating through texture atlas frames
        5. Putting it all together
      9. Centering the camera on a sprite
      10. Summary
    7. 3. Mix in the Physics
      1. Laying the foundations
        1. Adopting a protocol for consistency
        2. Organizing game objects into classes
          1. The icy tundra
            1. Adding the ground texture to Assets.xcassets
            2. Adding the Ground class
            3. Tiling a texture
            4. Running wire to the ground
          2. Adding the player's character
      2. Renovating the GameScene class
      3. Physics bodies and gravity
        1. Dropping like flies
        2. Solidifying the ground
      4. Exploring physics simulation mechanics
      5. Bumping bees into bees
        1. Movement with impulses and forces
      6. Summary
    8. 4. Adding Controls
      1. Retrofitting the Player class for flight
        1. The Beekeeper
        2. Updating the Player class
        3. Moving the ground
      2. Assigning a physics body to the player
        1. Creating a physics body shape from a texture
      3. Polling for device movement with Core Motion
        1. Implementing the Core Motion code
      4. Wiring up the sprite onTap events
        1. Implementing touchesBegan in the GameScene
        2. Larger than life
      5. Teaching our penguin to fly
      6. Listening for touches in GameScene
      7. Fine-tuning gravity
      8. Spreading your wings
      9. Improving the camera
      10. Pushing Pierre forward
      11. Tracking the player's progress
        1. Looping the ground as player the moves forward
      12. Summary
    9. 5. Spawning Enemies, Coins, and Power-Ups
      1. Introducing the cast
      2. Locating and adding the art assets
      3. Adding the Power-up Star
        1. Adding the Star class
      4. A new enemy - the Mad Fly
        1. Adding the MadFly class
      5. Another terror - Bats!
        1. Adding the Bat class
      6. Guarding the ground with the Blade
        1. Adding the Blade class
      7. Adding coins
        1. Creating the coin classes
      8. Organizing the project navigator
      9. Testing the new game objects
      10. Preparing for endless flight
      11. Summary
    10. 6. Generating a Never-Ending World
      1. Designing levels with the SpriteKit scene editor
        1. Separating level data from game logic
        2. Using custom classes in the scene editor
      2. Building encounters for Pierre Penguin
        1. Creating our first encounter
        2. Integrating scenes into the game
      3. Looping encounters for a never-ending world
        1. Building more encounters
        2. Updating the EncounterManager class
        3. Storing metadata in SKSpriteNodeuserData property
        4. Wiring up EncounterManager in the GameScene class
      4. Adding the Power-up Star at random places
      5. Turning bronze coins into gold coins
      6. Summary
    11. 7. Implementing Collision Events
      1. Learning the SpriteKit collision vocabulary
      2. Collision versus contact
      3. Physics category masks
      4. Using category masks in Swift
      5. Adding contact events to our game
        1. Setting up the physics categories
      6. Assigning categories to game objects
        1. The player
        2. The ground
        3. The Power-up Star
        4. Enemies
        5. Coins
      7. Preparing GameScene for contact events
      8. Viewing console output
      9. Testing our contact code
      10. Player health and damage
      11. Animations for damage and game over
        1. The damage animation
        2. The game over animation
        3. Collecting coins
      12. The Power-up Star logic
      13. Summary
    12. 8. Polishing to a Shine – HUD, Parallax Backgrounds, Particles, and More
      1. Adding a HUD
        1. Implementing the HUD
      2. Parallax background layers
        1. Adding the background assets
        2. Implementing a background class
        3. Wiring up backgrounds in the GameScene class
      3. Using the particle system
        1. Adding the circle particle asset
      4. Creating a SpriteKit particle file
      5. Configuring the path particle settings
        1. Name
        2. Background
        3. Texture
        4. Emitter
        5. Lifetime
        6. Position Range
        7. Angle
        8. Speed
        9. Acceleration
        10. Alpha
        11. Scale
        12. Rotation
        13. Color Blend
        14. Color Ramp
      6. Adding the particle emitter to the game
      7. Granting safety as the game starts
      8. Summary
    13. 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
      2. Wiring up the START GAME button
        1. Adding the restart game menu
          1. Extending the HUD
      3. Wiring up GameScene for game over
      4. Informing the GameScene class when the player dies
      5. Implementing touch events for the restart menu
      6. Adding music with AVAudio
        1. Adding sound assets to the game
        2. Playing background music
      7. 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 sound effects with SKAction
        4. Adding a mute button and volume slider
      8. Adding Options to the Menu Scene
      9. Summary
    14. 10. Standing out in the Crowd with Advanced Features
      1. Adding crates to smash open
      2. Creating the Crate particle effects
      3. Recycling emitter nodes with particle pools
      4. Wiring up crate contact events
      5. Creating the health power-up crate
      6. Spawning smashable crates that reward coins
      7. Summary
    15. 11. Introduction to SceneKit
      1. Creating a Scene with SCNScene
      2. Adding objects to the scene
        1. Adding a sphere
        2. Adding light sources
        3. Adding a camera to the scene
        4. Adding a floor
      3. Importing scenes from an external 3D application
      4. Creating the hero class and physics
      5. Adding an enemy and collision detection
      6. Adding a SpriteKit overlay
      7. Adding labels and buttons
      8. Adding touch interactivity
      9. Adding the Gameloop
        1. Setting a GameOver condition
        2. Checking for contact
      10. Adding wall and floor parallax
      11. Adding particles
      12. Adding character animation
      13. Summary
    16. 12. Choosing a Monetization Strategy
      1. Developing your marketing plan
        1. When to start marketing
        2. Marketing checklist
      2. Leveraging crowdfunding
        1. Pros and cons of crowdfunding
      3. Showing display ads for revenue
        1. The upsides to showing ads
        2. The downsides to showing ads
      4. Adding an AdMob Ad in the App
      5. Selling in-app purchases
        1. In-app purchase strategies
        2. A word about farming your players
      6. Adding In-App Purchases
      7. Localization in foreign markets
      8. Managing scope and completing projects
      9. Summary
    17. 13. Integrating with Game Center
      1. Authenticating the player's Game Center account
      2. Opening Game Center in our game
      3. Updating the leaderboard from the code
      4. Adding an achievement
        1. Creating a new achievement in iTunes Connect
        2. Updating achievements from the code
      5. Summary
    18. 14. Introduction to Spritekit with ARKit
      1. Requirements for the project
      2. Creating an AR Spritekit project
      3. Adding text and crosshair
      4. Adding anchors at random locations
      5. Adding custom sprite
      6. Registering touch controls to remove game objects
      7. Summary
    19. 15. Introduction to Scenekit with ARKit
      1. Going through the basic Scenekit/ARKit project
      2. Project setup and detecting a plane
      3. Adding touches
      4. Adding Game Objects
        1. Stop detecting planes
        2. Adding light source
        3. Adding ground node
        4. Adding Hero and Enemy
      5. Adding Score and Gameover text
      6. Finishing touches
      7. Summary
    20. 16. Publishing the Game on the App Store
      1. Creating the Bundle ID for the app
      2. Preparing the project
      3. Create the App in the itunesconnect portal
      4. Upload the App and submit for review
      5. Summary
    21. 17. Multipeer Augmented Reality
      1. Multipeer connectivity framework overview
      2. Creating the multipeer session class
      3. Creating a UI for the app
      4. Setting outlets and adding variables
        1. Initializing the view
        2. Update session and tracking
        3. Hosting and joining the session
        4. Sending and receiving data
        5. Initializing a multipeer session
      5. Testing the application
      6. Summary
    22. Other Books You May Enjoy
      1. Leave a review – let other readers know what you think
    23. Index