Sprite Kit makes it easy to build compelling, high-performance games for iOS and OS X using the same object-oriented principles, design patterns, and Objective-C language that you use to build other apps. To demonstrate what you can accomplish with Sprite Kit, we’ve created the complete Adventure game:
This code:Explained provides an overview and guided tour through the Adventure Xcode project. By working through this document, you’ll see how we used Sprite Kit nodes, actions, and physics, to build a level in which you play the role of an intrepid hero walking through the walls of a forested maze. There are several different characters and basic sprites that make up the game, including your hero, caves that spawn the evil goblins you’ll encounter along the way, not to mention the club-wielding boss at the end of the level.
What You’ll Learn from This code:Explained
This document is designed to be read alongside the Adventure Xcode project, giving you context and additional explanation as you work through the sample code.
We built Adventure using a single Xcode project that targets both iOS and OS X, which means we can share code and assets across the two platforms. Combining both targets into one project also makes it very easy to test the game on OS X, in the iPhone Simulator, and on an iOS device, simply by choosing the relevant target before building.
There are 17 classes used to represent the different elements in Adventure, including the scene, the animated characters, static trees and caves, and the artificial intelligence that governs how enemies chase your hero through the maze. In addition, you’ll find a few platform-specific classes and resources that represent simple containers for the game, as well as a large number of shared art and visual effects assets.
At launch, we preload all the art resources necessary to play Adventure so that we don’t have to read them from disk during gameplay (which could lead to annoying pauses or dropped frames). After the resources are loaded, we add in the different elements that make up the level, including the background tiles, physics bodies for the maze walls, the caves that spawn goblins, the level boss, and the static trees located all around the level.
After we’ve loaded all the resources and added the elements to the scene, the rest of the work for the game takes place mostly in the update loop. For each frame, a Sprite Kit scene automatically runs through several steps in order, including evaluating actions and simulating physics, before it renders the content in the presenting view. We tie into this loop between the various stages to perform tasks such as updating each character, updating enemy AI, and moving the camera to center the world on the hero. Each time we need to move the camera, we update the parallax offsets of the trees and caves.
The hero character in Adventure is controlled by you. When playing the game on OS X, you control the hero from the keyboard; on iOS, you touch the screen to indicate where you want the hero to move. The goblins and boss characters have artificial intelligence, which causes them to chase and attack you when you are within a predefined distance.
The heroes, goblins, and level boss are all animated, even when idle. Each of these characters has different sequences of animation frames used for standing, walking, attacking, being attacked, and dying. Animations are triggered in various ways such as user input, actions dictated by the artificial intelligence, and collisions between objects.
Each Adventure character has a corresponding physics body, which detects collisions with other characters, the walls of the maze, or with projectiles. Some of the physics behavior in the game—for example, being prevented from walking through walls—is automatically handled by Sprite Kit; all we need to do is configure bit masks on physics bodies.
Some collisions trigger other actions. For example, when a hero projectile collides with a goblin, it starts that goblin’s death sequence and increases your score. For these sequences, we rely on Sprite Kit’s physics delegate mechanism—each time an important collision occurs, the scene receives a callback indicating the different bodies involved.
What You Need to Know Before Reading This Document
Building a game with Sprite Kit involves all the same tools and techniques you use when building other apps using Objective-C and Cocoa or Cocoa Touch. For example, Adventure uses a storyboard for iOS, and a
MainMenu.xib file for OS X, to create a simple app interface containing the view in which the game content is displayed. If you don’t already have a good understanding of app development for iOS and OS X, you should read Start Developing iOS Apps Today or Start Developing Mac Apps Today.
To get the most out of this document and the Adventure sample project, read at least the early chapters of SpriteKit Programming Guide. You’ll get a general understanding of the Sprite Kit framework and learn the basic elements used to display information on screen—such as views, scenes, nodes, actions, and physics bodies, all of which are used in Adventure.