Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

iOS 7’s SpriteKit is Quietly Changing the Future of Mobile Gaming — Here’s How to Develop Games On It

20 Feb 2014 1  
In this article I’m going to take a close look at the SpriteKit code, show how it was used in a recent game, and finally, end with a mini-tutorial for building a SpriteKit game from scratch.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

We’re Kii (www.kii.com), a mobile backend, distribution, analytics platform and a new Game Cloud service. As part of Game Cloud’s launch, we’re challenging game developers working on a new mobile game to add Game Cloud features into it, and compete to win $10,000, along with distribution in the world’s largest and highest monetized mobile markets: China and Japan.

Now lets look at Spritekit and how its changing the mobile gaming landscape

Introduction: Why Game Mobile Developers Need to Know About SpriteKit

iOS 7 has several powerful new features for mobile game developers which were missed amidst the fanfare of the OS launch, but every game developer needs to know about them now. These include new libraries which bring complex physics, animations, and particles to app developers out-of-the-box, and can be utilized just as easily as all the core frameworks they already know and love. The largest of these features is a new framework called SpriteKit. As the name indicates, this is a framework for creating 2D games, and odds are, SpriteKit is going to become a major way we develop mobile games over the next few years. Here’s why:

SpriteKit’s new features coincide nicely with the iPhone 5S, which is the first smartphone to provide 64-bit architecture. In combination with new hardware, developers will be able to create some seriously awesome games that will run smoother than ever with current and future generations of iOS devices. What’s more, iOS7 is already the most quickly adopted mobile operating system ever — just one month after release, it was already on 64% of devices. This means that there are tens of millions of devices waiting for your new SpriteKit game. The framework can also be utilized on the latest (and FREE!) version of OSX Mavericks, which means you can develop SpriteKit games for Mac users. Not only that, the insider word is Apple TV will become a standalone device, or at the very least, bring iOS-like gaming to larger screens and living rooms around the world. With all these updates, it’s very likely that SpriteKit will become a major tool for game developers.

So with all that in mind, I’m going to take a close look at the SpriteKit code, show how it was used in a recent game, and finally, end with a mini-tutorial for building a SpriteKit game from scratch.

Inside SpriteKit: Features (and Drawbacks) for Game Developers

SpriteKit has a number of core components for game developers to help them create powerful and fun games quickly and easily. Further, this new framework is baked into the core iOS and OSX SDKs. That means SpriteKit’s OpenGL-based renderer is deeply integrated with the hardware, and will run more efficiently than a third-party library.

Let’s take a look at SpriteKit’s core features:

A native physics engine

Adding physics to your stage couldn’t be simpler. Just define the attributes when you create your world and the bodies within it will react accordingly. Here is a quick example to track our objects and add gravity with a downward vector of 4.0 to our scene:

// MyScene : SKScene
self.physicsWorld.gravity = CGVectorMake(0, -4.0f);

Once the world is defined, add attributes to the physics bodies within your game and they will react to their environment. This could be as simple as:

_ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:myPath];
_ship.physicsBody.mass = 2.0f;
_ship.physicsBody.friction = 0.4f;

Textures & Sprites

SpriteKit deals with textures and sprites in a very simple way. If you’re an existing iOS developer, you’ll feel very comfortable with this method of creating a textured sprite:

SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"rocket.png"];
spaceship.position = CGPointMake(100,100);
[self addChild: spaceship];

The SpriteKit framework automatically handles all the lower-level functionality needed to load the texture into your game and display it properly. Sometimes you might still want more control (i.e. if you are loading in a large number of textures and are seeing performance degradation). Fortunately, the framework still allows you to manage this process yourself if you prefer.

Particle System Editor

This is a great feature that is now baked right into Xcode with a handful of templates to utilize within your own game with little to no effort. It comes with particles like smoke, fire, snow, and more – all of which are fully customizable to give you the exact look and feel you need for your game. Use the utility panel and the real-time preview to tweak your particle emitters in no time at all.

Some of these features may be familiar to existing game developers, as they are the cornerstones to a great 2D game. Prior to iOS7, these could only be found in 3rd party libraries such as cocos2d.

By the way, the particle system editor is built right into Xcode’s IDE, allowing developers to generate a number of particle systems without having to use external editors. This commitment to game development is likely just the start, and we should expect to see XCode and the framework be more accommodating with each iteration.

Drawbacks & Advantages: Compatibility & OS Lock-In, versus Ease & Speed

No solution is ever 100% perfect and SpriteKit is no exception. There are some topics that just aren’t covered in the current version of the framework, especially when compared to a more mature framework like cocos2d. And from a capability standpoint, developers don’t have the ability to run any custom OpenGL code. This can be a hinderance if you’re really trying to tweak your graphics or optimize your framerate.

The largest issue, however, is that your game code is locked into the iEcosystem. SpriteKit currently cannot be exported to other platforms like cocos2d or Unity – and it’s unlikely that the option will ever be available moving forward. This doesn’t mean you can’t port to other popular platforms, but doing so will add cost and time to your project.

In any case, many app developers already choose to go iOS-first, building out their initial versions on iOS without worrying about other devices or operating systems. This greatly reduces development and testing time, which also reduces cost and risk for the project. From that perspective, SpriteKit is a great way to test the market on iOS first, and see if it’s viable to expand your game to other mobile platforms.

Another advantage to SpriteKit is that it brings the easy-to-use Apple API design to 2D game development, leveraging all the power of the device. This allows a myriad of objective-c and iOS developers to dip into the game development world without leaving their ecosystem. So whether you are taking your first steps into game development or want to build a great 2D game optimized for iOS – this framework could be for you.

Speaking of which, let’s take a look at a new iOS game made with SpriteKit:

SpriteKit Case Study: BattleBats

BattleBats is a recently-released 2D game which chose to go iOS-first, using the MVP (Minimum Viable Product) methodology to utilize analytics and user feedback to pivot and improve the game. The game is pretty much a mix between Pong and Breakout — but on steroids. It even comes with a multi-player mode, so you compete with up to 3 other players. It was built entirely within SpriteKit, and utilizes its physics engine for basic gameplay (hitting the ball, bouncing off the blocks, etc), sprites for the objects within the game (ball, paddle, items, etc), and particles for some of the advanced visual effects.

BattleBats is now live on iTunes for all iOS devices

BattleBats incorporated Kii Cloud, our framework for adding a cloud backend and custom analytics, and was a winner of our recent app competition, so I’ve been chatting with developer Michael Burks on why he chose SpriteKit as his framework. Basically, speed and efficiency:

I wanted to build a fun multi-player game but didn’t want to worry about re-inventing the wheel by dealing with lower-level physics or building my own cloud. I used SpriteKit and a tool called Kii Cloud to create this game in a matter of weeks — getting it out into the market quickly to see what people enjoyed (or didn’t) about it.

Video Tutorial: Game Development with SpriteKit

Now that you’re (hopefully!) ready to try your own hand at SpriteKit development, here’s an introductory tutorial video. Narrated by myself with help from other Kii devs, it’s part of our ongoing video series for developing an entire cloud-connected game for iOS. This tutorial will introduce you to the SpriteKit physics engine, and show you how to create a SpriteKit project:

By the way, this project is open-source and a great way to dive into game development, or learn more about SpriteKit first-hand. Follow the project here.

SpriteKit/Cloud development questions? Please feel free to post them in Comments, or find me on Twitter @cjbeauchamp.

Kii Cloud is a full-stack mobile backend to accelerate iOS, Android, HTML5 and Unity app development. Optional ad network integration is available to help you monetize. Get started with a free account at developer.kii.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here