Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

How to Make a Game Like Candy Crush with SpriteKit and Swift: Part 4

In this epic tutorial you'll learn how to make a tasty match-3 game like Candy Crush with SpriteKit and Swift. Now updated for Xcode 7.3 and Swift 2.2.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1194-how-to-make-a-game-like-candy-crush-with-spritekit-and-swift-part-4

One of the coolest tuto i have done.
Thx man.
One question: Why do you make GameViewController handle some “game logic” instead of GameScene?
Like “handleSwipe” “handleMatches”, “shuffle”.
I think they fit better in GameScene.

Hey Maikel. Thanks for you kind words :]

As for your question – I can’t speak for the original author, but I chose to keep the functions inside the GameViewController as they relate directly to actions happening in the view (such as clicking the Shuffle button). What’s more, when decoupling the logic from the GameScene itself, you also have the option of expanding your game further down the line by adding new scenes, while still using the same GameViewController.

But you could definitely argue for both cases :]

Thank you for this amazing series. I like all RW tutorials because there is always something new to learn, but these 4 tutorials were exceptional. I was particularly impressed with the elegance of the code that created rounded edges near the end of Part 4. I am going to try again from the top because this series was so impressive. :smile:
EDIT: The second time was more fun with my own twists and flares added. In any case, I have a few questions:

  1. I see that the GameViewController makes a direct connection between GameScene’s level property and the level (the model). And I thought that in MVC, M and V were supposed to be separated by C. Could you enlighten me on this? Am I taking the separation of M and V too literally? Is this kind of linkage acceptable as long as V is only peeking into M, and not changing M directly?
  2. Would you still use MVC if it were a game that involved more sprites, actions, particles, etc — like shoot 'em up type of games?
  3. I noticed that the memory usage was edging up as I played this game. I was using my first version, the one I followed your instructions (not the one I added my own flares). For example, when the game started, it was 134.2MB. When I completed the first level, it was 139.2MB. After the second, 141.4MB. After the third, 143.1MB. The fourth level is virtually impossible, but after the first failure, it was 143.5MB, and after the second failure, it was 145.3MB. Will the excess memory be purged automatically when it gets too high, or is something leaking somewhere? I think the code has all the necessary removeFromParent() command.
    I’m not quite a beginner, and I consider myself somewhere between a beginner and an intermediate Swift programmer. Any help would be appreciated. Thank you.
1 Like

Excellent! thanks for putting this together.

Here’s a fix if anyone is having trouble after converting to Swift 3 on line

let value = Int(topLeft) | Int(topRight) << 1 | Int(bottomLeft) << 2 | Int(bottomRight) << 3

with error saying: “cannot invoke initializer for type Int with an argument list of type Bool”

It basically says you cannot convert a Bool value into an Int.

Solution, we need to use the “hashValue” of the bool:

let value = Int(topLeft.hashValue) | Int(topRight.hashValue) << 1 | Int(bottomLeft.hashValue) << 2 | Int(bottomRight.hashValue) << 3

Hoped I helped someone the headache :]

1 Like

Yes Sandor, it was helpful, thanks!
Strange that no one check this before :slight_smile:

This has been such an incredible tutorial! I wonder if it would be possible to show how to build an additional level and include a world map?

Thanks @rydevera :] While the tutorial series won’t have those two topics mentioned this time around, I encourage you to look into it yourself :] ! If you look at the provided Level_{num}.json files it should be clear how the layout of the maps are created. As for the World Map, I suggest you look into the SpriteKit Scene Editor.

I wrote a tutorial on it a while back, which can be found here. It doesn’t show you how to make a World Map, but just how to work with the editor in general. Note: It’s written for Swift 2 and Xcode 7.

1 Like

Awesome! Thank you I will definitely look at this!

1 Like

Hey

Amazing tutorial thanks !!

Just one question to clarify. I was following along with no problems with Swift 3.

But if I change the targetScore on the jSON files I cannot get it to display its text on the label.

Any ideas ?? Has anyone tried changing the target score ??

I am having some trouble with your code. I followed the tutorial and even downloaded the final build than uploaded it. Whenever I try to run the code, the game loads but no cookies are actually populated.

Thank you for this incredible tutorial.But i’ve got a question.In the tutorial, says MVC pattern and the gameScene is like the view only handle the present & animate.Why the gameScene can hold the ‘level’ to be its property? In my opinion,the level obj is a model,why the view can directly hold the model?

no one has answered this just to help out anyone who is working on this. the reason you are getting this error is because you forgot to do the following.

make two changes to addTiles():

Replace “Tile” with “MaskTile”
Replace tilesLayer with maskLayer

Hey I could use a little help understanding the whole L Shaped cookie detect…where would that code actually go ?

Would I need to create a new function or would it go into remove Matches and use the chain sets below ??

    let horizontalChains = detectHorizontalMatches()
    let verticalChains = detectVerticalMatches()

Great tutorial! Super helpful.

Great tutorial! I was able to find L and T shapes, I have added them to the chain set. But, I am trying to add a special sprite (cookie that would break an entire row or column). After finding an L Shape I am able print the cookie info to the console but I am confused as to how to add this one special sprite to the the 2D grid. Do you have any advice for me? Can you point me to a concept I might have left behind? Thank you.

hey were you able to figure this out?

Has anyone tried to implement at least one of these functions?

  • Special cookies when the player matches a certain shape. For example, Candy Crush Saga gives you a cookie that can clear an entire row when you match a 4-in-a-row chain.
  • Detection of special chains, such as L- or T-shapes, that reward the player with bonus points or special power-ups.
  • Boosts, or power-ups the player can use any time they want. For example, one boost might remove all the cookies of one type from the screen at once.
  • Jelly levels: On these levels, some tiles are covered in jelly. You have X moves to remove all the jelly. This is where the Tile class comes in handy. You can give it a Bool jelly property and if the player matches a cookie on this tile, set the jelly property to false to remove the jelly.
  • Hints: If the player doesn’t make a move for two seconds, light up a pair of cookies that make a valid swap.
  • Shuffle the cookies automatically if there are no possible moves.

Can someone share this experience? I will be very grateful. Thank’s
My Email: arturskachkov2014@gmail.com

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]