How To Make a Game Like Cut the Rope With SpriteKit
In this tutorial, you’ll learn how to build a game like Cut the Rope with SpriteKit in Swift, complete with animations, sound effects and physics! By Brody Eller.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How To Make a Game Like Cut the Rope With SpriteKit
30 mins
- Getting Started
- Adding the Crocodile to the Scene
- Adding the Prize
- Working With Physics
- Adding Vines
- Defining the Vine Class
- Adding Joints to the Vines
- Snipping the Vines
- Handling Contact Between Bodies
- Animate the Crocodile's Chomp
- Resetting the Game
- Ending the Game
- Adding Sound and Music
- Adding the Background Music
- Adding the Sound Effects
- Getting Rid of an Awkward Sound Effect
- Adding Some Difficulty
- Where to Go From Here?
Getting Rid of an Awkward Sound Effect
Did you discover a bug? If you miss the croc, the splashing sound plays multiple times. This is because you trigger "level complete" logic repeatedly before the game transitions to the next scene. To correct this, add a new state property to the top of the class:
private var isLevelOver = false
Now modify update(_:)
and didBegin(_:)
by adding the following code at the top of each of them:
if isLevelOver {
return
}
Finally, inside the other if
statements of the same methods, add some code to set the isLevelOver state to true:
isLevelOver = true
Now, as soon as the game detects that isLevelOver
is set, either because the pineapple hit the ground or because the croc got its meal, it'll stop checking for the game win/lose scenarios. It won't keep repeatedly trying to play those sound effects.
Build and run. There are no awkward sound effects anymore!
Adding Some Difficulty
After playing a few rounds, the game seems a bit too easy. You'll quickly get to the point where you can feed the croc with a single well-timed slice through the three vines.
Make things trickier by using the value in Constants.swift, CanCutMultipleVinesAtOnce
.
In GameScene.swift, add one last property at the top of the GameScene
class definition:
private var didCutVine = false
Now locate checkIfVineCut(withBody:)
and add the following if
statement at the top:
if didCutVine && !GameConfiguration.canCutMultipleVinesAtOnce {
return
}
Add this code to the bottom of the same method, inside the if
statement:
didCutVine = true
Just to keep things together, find the touchesBegan(_:with:)
, and add the following line:
didCutVine = false
This way, you reset didCutVine
whenever the user touches the screen.
Build and run again.
You should see that it's now only possible to snip one vine each time you swipe. To cut another, you have to lift your finger and then swipe again.
Where to Go From Here?
You can download the completed version of the project using the Download Materials button at the top or bottom of this Cut The Rope with SpriteKit tutorial.
Don't stop now! Try adding new levels, different vines and maybe even a score display and timer.
If you’d like to learn more about SpriteKit, be sure to check out our book, 2D Apple Games by Tutorials.
If you have any questions or comments, feel free to join in the discussion below!