A simple “shoot-em-up” game with Sprite Kit and Swift, part one: Last things first – the complete project

by Joey deVilla on August 10, 2014

In case you were wondering: yes, this code has been updated so that it’s compatible with the iOS APIs that came after the XCode 6 beta period.

a simple shoot em up game

swift kick

Here’s a fun programming exercise that you might want to try if you’ve been meaning to learn both Swift and game development for iOS: building a simple “shoot ’em” game using both!

alienThis game features alien slugs (pictured on the left) that appear at random “altitudes” onscreen and travel from left to right or right to left, right in the line of fire of your ship, located near the bottom of the screen at the center. The player shoots at them by tapping on the screen; the locations of the player’s taps determine the direction of the shots.

As I said, it’s a simple game, but it introduces a fair number of concepts including:Spaceship

  • Drawing, positioning, scaling and moving sprites with Sprite Kit
  • Operator overloading and extensions in Swift
  • Vector math
  • Responding to user taps
  • Figuring out when two sprites collide
  • Playing sound effects and a continuous soundtrack

I thought I’d do things “backwards” with this article series by giving you what you need to complete the project first, and then exploring the code section by section in subsequent articles. That way, you can immediately see what the final result should look like and do some experimenting and exploring on your own.

How to build the simple shoot ’em up game

  1. Create a new iOS project in Xcode based on the Game template. Give the project any name you like; I named mine SimpleShootEmUp.
  2. Make sure the app uses landscape orientation only by selecting your the project and target in the Project Navigator, and then in the Deployment section, make sure that Portrait is unchecked and Landscape Left and Landscape Right are checked.
  3. Download the .zip file of resources for the game, then unzip it. There’ll be two folders, Images.atlas and Sounds, which you should drag into your project. A dialog box will appear; make sure that Copy items into destination group’s folder (if needed) is checked, and that your project’s target is selected.
  4. And finally, replace the contents of the GameScene.swift file that was automatically generated for your project with the code below. I’ve commented it rather heavily to help you get a better idea of what’s going on:

 

Once that’s done, the game’s ready to run. Go ahead and run it, play, explore the code, experiment, and learn! In the next installment, I’ll start explaining the code, starting with some Sprite Kit basics.

Credit where credit is due

  • Much of the code is a Swift adaptation of the code in Ray Wenderlich’s September 2013 article, Sprite Kit Tutorial for Beginners. If you’re serious about learning iOS development, you want to bookmark Ray’s site, RayWenderlich.com, the home of not just tutorial articles, but tutorial videos, very in-depth books, and even starter kits for budding iOS game developers.
  • The alien images were taken from Backyard Ninja Design’s Free Stuff page, specifically the set of sprite from their “Crapmunch” game.
  • The ship image is the one included in the example game that’s automatically created when you generate a game project.
  • The background music is FU4, created by Partners in Rhyme, and can be found on their Free Royalty-Free Music Loops page.

{ 4 comments… read them below or add one }

1 ron333 September 15, 2014 at 2:00 pm

Apple has now made the class SKPhysicsBody an optional, i.e. SKPhysicsBody?

Thus, to access any of its properties you need to use unwrapping.
alien.physicsBody!.dynamic = true

2 Joao Henrique September 24, 2014 at 10:29 am

I am trying to download the resources but Google Drive tells me that you had too many downloads, is there any other way I can download it?

3 Joey deVilla October 3, 2014 at 10:57 pm
4 Joe November 17, 2014 at 9:35 am

the destroyAlien() will be called more then one time. how to adjust the code to do this?

Leave a Comment

{ 3 trackbacks }

Previous post:

Next post: