SpriteKit and Inverse Kinematics with Swift

In this tutorial, learn how to use Sprite Kit’s inverse kinematics to make a ninja punch and kick dynamically! By Jorge Jordán.

Leave a rating/review
Save for later
Share
You are currently viewing page 6 of 6 of this article. Click here to view the first page.

Gratuitous Background Music

Last but not least, every ninja game needs good background music, so switch to GameViewController.swift and add the following import at the top of the file:

import AVFoundation

This gives you access to the AVFoundation framework, which is necessary to play background music.

Still in GameViewController.swift, add the following property to the GameViewController class:

var audioPlayer: AVAudioPlayer?

Next, you'll write a function that plays your background music. Add the following function to the GameViewController class:

func startBackgroundMusic() {
  if let path = Bundle.main.path(forResource: "bg", ofType: "mp3") {
    audioPlayer = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: path), fileTypeHint: "mp3")
    if let player = audioPlayer {
      player.prepareToPlay()
      player.numberOfLoops = -1
      player.play()
    }
  }
}

The function looks for the background music file bg.mp3 in the resource path, loads it into memory and plays it indefinitely.

Note: The AVAudioPlayer() initializer can throw errors that need to be handled. As the audio resource is provided on the project no error will be thrown so that's why you wrote try!, in order to disable error propagation. For more details, refer to the documentation.

Note: The AVAudioPlayer() initializer can throw errors that need to be handled. As the audio resource is provided on the project no error will be thrown so that's why you wrote try!, in order to disable error propagation. For more details, refer to the documentation.

Finally, add the following line to the bottom of viewDidLoad() in GameViewController to play the music upon loading the main view:

startBackgroundMusic() 

That's it! Build and run the project, and strike shurikens to the beat of your new background music.

Ninja

Where to Go From Here?

You can download the complete project for this tutorial here.

At this point, you should have a solid grasp of the key concepts of inverse kinematics in Sprite Kit.

Inverse kinematics is a powerful technique that brings a new level of dynamism to traditional character animation. There are many possible goal-oriented scenarios where inverse kinematics would apply an additional touch of realism—from having a snake whip its neck around to face the user, to a goalkeeper adjusting both hands to catch an oncoming ball, to a wizard dexterously waving a wand in mid-air—it's all up to your imagination!

In case you’re wondering, 3D inverse kinematics is also available in Scene Kit, Apple's 3D Graphics API. The feature allows you to blend inverse kinematics on top of existing animations, bringing an even greater sense of realism.

If you'd like to learn more, you should check out our book 2D iOS Games by Tutorials. The book teaches you everything you need to know to make 2D iOS games, by making a series of mini-games like this one, from an action game to a puzzle game to a tower defense game.

I hope you enjoyed this tutorial, and I look forward to seeing how you use inverse kinematics to spice up character animations in your own games!

Credits: Art by Vicki Wenderlich from gameartguppy.com. Background music from Kevin MacLeod. Hit Sound Effect from Mike Koenig, and Miss Sound Effect from Mark DiAngelo.

Jorge Jordán

Contributors

Jorge Jordán

Author

Over 300 content creators. Join our team.