Starting Sprite Kit, Creating a Running Man

running running2
Images from the tutorial I followed, creating a man that runs across the screen. (McIlrath 2014)

To get a greater understanding of creating game using Sprite Kit and Swift, I followed this tutorial to understand the basics of the language and the Sprite Kit library.

touches
Sprite Kit’s class for detecting when the screen is tapped.

Sprite Kit provides a graphics rendering and animation infrastructure that you can use to animate arbitrary textured images, or sprites. Sprite Kit uses a traditional rendering loop where the contents of each frame are processed before the frame is rendered. Your game determines the contents of the scene and how those contents change in each frame. Sprite Kit does the work to render frames of animation efficiently using the graphics hardware. Sprite Kit is optimized so that the positions of sprites can be changed arbitrarily in each frame of animation.

Sprite Kit is one of the easiest ways to start making a game for IOS as it has a lot of in-built functionality. A quick way to start working in Sprite Kit and Swift is to modify the template that Swift provides you. This includes some classes which are essential to game design, such as an Update class which is calculates the changes needed before in order for the game to update the visuals properly.

I did this to start off my game. If you build the template you are provided with a “game” that create spaceships if you tap the screen. After deleting the existing code you can introduce your own artwork into the scene by using SKSpriteNode.

 let groundTexture = SKTexture(imageNamed: "ground")

The code I used to implement the ground into the scene

This SpriteKit class allows you to not only add images to your scene, but also add other functionality such as adding physics to it. The node itself is not itself an image, although it needs an image passed into it to be called. This means you can animate the node by using several different textures.

 func runForward()
  {
    let hero_run_anim = SKAction.animateWithTextures([
      SKTexture(imageNamed: "runningman1"),
      SKTexture(imageNamed: "runningman2"),
      SKTexture(imageNamed: "runningman3"),
      SKTexture(imageNamed: "runningman4"),
      SKTexture(imageNamed: "runningman5"),
      SKTexture(imageNamed: "runningman6"),
      SKTexture(imageNamed: "runningman7"),
      SKTexture(imageNamed: "runningman8"),
      SKTexture(imageNamed: "runningman9"),
      SKTexture(imageNamed: "runningman10"),
      SKTexture(imageNamed: "runningman11"),
      SKTexture(imageNamed: "runningman12"),
      SKTexture(imageNamed: "runningman13"),
      SKTexture(imageNamed: "runningman14")
      ], timePerFrame: 0.06)
    
    let run = SKAction.repeatActionForever(hero_run_anim)
    
    hero.runAction(run, withKey: "running")
  }

Code I used to animate the man. I used Sprite Kit’s built in functions SKAction.animateWithTextures to create an animation for the man

After playing around with this tutorial I had a good understanding of how to build a scene using SKSpriteNodes and how to animate them. I used z-positioning to overly the man on top of the floor and the background layer. Next I need to learn about movement and physics in order to make the scene move more dynamically. I will follow some more tutorials to get a better idea of the process involved before I start coding the proper game.

McIlrath, C., 2014. Swift Programming Blog – Getting Started with Sprite Kit Physics. [online] SwiftCast.TV. Available from: https://swiftcast.tv/articles/getting-started-with-sprite-kit-physics [Accessed 1 Mar. 2015].