# How to Make a Game Like Candy Crush with Swift Tutorial: Part 1

**URL:** <https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746>\
**Category:** 🎓 Courses & Articles\
**Created:** [February 28, 2016, 8:03pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746 "2016-02-28T20:03:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [February 28, 2016, 8:03pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/1 "2016-02-28T20:03:08Z")

</div>

Learn how to make a game like Candy Crush for your iPhone in this 2-part tutorial series - and get a great Swift tutorial along the way!

* * *
 This is a companion discussion topic for the original entry at [http://www.raywenderlich.com/75270/make-game-like-candy-crush-with-swift-tutorial-part-1](http://www.raywenderlich.com/75270/make-game-like-candy-crush-with-swift-tutorial-part-1)

---

<div class="post-metadata">

**Author:** ![radamsdigital](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@radamsdigital](https://forums.kodeco.com/u/radamsdigital)\
**Post date:** [February 29, 2016, 1:40am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/2 "2016-02-29T01:40:23Z")

</div>

I’m making a match game using some of the ideas from this tutorial. The Cookies.swift file uses ‘static func random() → CookieType’ to create random numbers for positioning the sprites.

This method for random numbers allows repeats and doesn’t guarantee utilization of all members of the array. Does anyone know a way to use arc4random\_uniform in this way to assure no repeats until all are used?

The line of code is ‘return CookieType(rawValue: Int(arc4random\_uniform())+1)!’

Any guidance will be appreciated.

---

<div class="post-metadata">

**Author:** ![khanh\_pham](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/khanh_pham/32/103433_2.png) [@khanh\_pham](https://forums.kodeco.com/u/khanh_pham)\
**Post date:** [March 25, 2016, 9:20am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/3 "2016-03-25T09:20:14Z")

</div>

I follow tutorial until “Build and run, and you should finally see some cookies:”  
I build and run project but sometime cookies appeared on screen but sometime not, it just blank screen (without changing code).  
Can anyone explane what do i do wrong ?

---

<div class="post-metadata">

**Author:** ![einharch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/einharch/32/81494_2.png) [@einharch](https://forums.kodeco.com/u/einharch)\
**Post date:** [March 27, 2016, 4:52pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/4 "2016-03-27T16:52:48Z")

</div>

Latest versions of Xcode can be a bit buggy, and possible problems as follow:

1. The texture aren’t fully copied to the device/simulator
2. The size of the sprite/tile is different from the texture of the node (cookie)

Possible solutions:

1. Clean, delete derived, rebuild
2. If you aren’t copy/pasting the tutorial (aka if you’re using your own textures/images), be sure to resize each SKTextureNode … One solution is to use the convenience method `SKSpriteNode(texture: SKTextureAtlas(named:).textureNamed(name:), size:)`

---

<div class="post-metadata">

**Author:** ![doyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/doyle/32/102075_2.png) [@doyle](https://forums.kodeco.com/u/doyle)\
**Post date:** [March 28, 2016, 2:48am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/5 "2016-03-28T02:48:54Z")

</div>

khanh\_pham, had similar issue, in GameScene.swift changed order of addChild so cookies and tiles show in front of background instead of behind:

override init(size: CGSize) {  
super.init(size: size)  
anchorPoint = CGPoint(x: 0.5, y: 0.5)  
let background = SKSpriteNode(imageNamed: “Background”)  
addChild(background)  
addChild(gameLayer)  
let layerPosition = CGPoint(  
x: -TileWidth \* CGFloat(NumColumns) / 2,  
y: -TileHeight \* CGFloat(NumRows) / 2)  
tilesLayer.position = layerPosition  
cookiesLayer.position = layerPosition  
gameLayer.addChild(tilesLayer)  
gameLayer.addChild(cookiesLayer)  
swipeFromColumn = nil  
swipeFromRow = nil  
}

---

<div class="post-metadata">

**Author:** ![khanh\_pham](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/khanh_pham/32/103433_2.png) [@khanh\_pham](https://forums.kodeco.com/u/khanh_pham)\
**Post date:** [March 28, 2016, 4:26am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/6 "2016-03-28T04:26:16Z")

</div>

I just find out that you just need to stop and clean project then build run again will fix the problem. The reason may be swift do not build complete.

---

<div class="post-metadata">

**Author:** ![einharch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/einharch/32/81494_2.png) [@einharch](https://forums.kodeco.com/u/einharch)\
**Post date:** [March 29, 2016, 3:51am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/7 "2016-03-29T03:51:15Z")

</div>

@hollance  
One question thought, preparing for Swift 3 where C-style for loops are deprecated, any idea on the best way to “swiftify” these kind of loops?

`for var i = column - 1; i >= 0 && cookies[i, row]?.cookieType == cookieType; --i, ++horzLength { }`

I was thinking of something like  
`for i in (0 ..< column).reverse() where (monsters[i, row]?.monsterType == monsterType)`

then increment the variable, but it obviously fails, as the “where” statement doesn’t apply simultaneously on the initial parameter.

Any clue?

---

<div class="post-metadata">

**Author:** ![jj\_tagy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jj_tagy/32/102075_2.png) [@jj\_tagy](https://forums.kodeco.com/u/jj_tagy)\
**Post date:** [April 16, 2016, 6:24am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/8 "2016-04-16T06:24:16Z")

</div>

I did the following which seems to work. Not sure if it is the accepted solution.

```
private func hasChainAtColumn(column: Int, row: Int) -> Bool {
    let cookieType = cookies[column, row]!.cookieType
    
    var horzLength = 1
    for index in (column - 1).stride(through: 0, by: -1) {
        if cookies[index, row]?.cookieType == cookieType {
            horzLength += 1
        }
        else {
            break
        }
    }
    for index in (column + 1).stride(to: NumColumns, by: 1) {
        if cookies[index, row]?.cookieType == cookieType {
            horzLength += 1
        }
        else {
            break
        }
    }
    if horzLength >= 3 {
        return true
    }
    
    var vertLength = 1
    for index in (row - 1).stride(through: 0, by: -1) {
        if cookies[column, index]?.cookieType == cookieType {
            vertLength += 1
        }
        else {
            break
        }
    }
    for index in (row + 1).stride(to: NumRows, by: 1) {
        if cookies[column, index]?.cookieType == cookieType {
            vertLength += 1
        }
        else {
            break
        }
    }
    return vertLength >= 3
}

```

---

<div class="post-metadata">

**Author:** ![banjochris](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@banjochris](https://forums.kodeco.com/u/banjochris)\
**Post date:** [May 15, 2016, 7:59pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/10 "2016-05-15T19:59:44Z")

</div>

I’m trying to build this game in xCode 7.3.1 and I’m getting the following errors:

Use of unresolved identifier ‘rawValue’ in the following code block:

var spriteName: String {  
let spriteNames = [  
“Croissant”,  
“Cupcake”,  
“Danish”,  
“Donut”,  
“Macaroon”,  
“SugarCookie”]

```
return spriteNames[rawValue - 1]

```

}

Type ‘Cookie.CookieType’ does not conform to protocol ‘CustonStringConvertible’ in the following code block:

enum CookieType: Int, CustomStringConvertible {  
case Unknown = 0, Croissant, Cupcake, Danish, Donut, Macaroon, SugarCookie

}

Note that CustomStringConvertible was recommended to replace Printable.

Here is the entire class code for context:

```
import SpriteKit

class Cookie: CustomStringConvertible {
    var column: Int
    var row: Int
    let cookieType: CookieType
    var sprite: SKSpriteNode?
    
    init(column: Int, row: Int, cookieType: CookieType) {
        self.column = column
        self.row = row
        self.cookieType = cookieType
    }
    
    enum CookieType: Int, CustomStringConvertible {
        case Unknown = 0, Croissant, Cupcake, Danish, Donut, Macaroon, SugarCookie
        
    }
    
    var spriteName: String {
        let spriteNames = [
            "Croissant",
            "Cupcake",
            "Danish",
            "Donut",
            "Macaroon",
            "SugarCookie"]
        
        return spriteNames[rawValue - 1]
        //return spriteNames.rawValue - 1
    }
    
    var description: String {
        return "type:\(cookieType) square: (\(column),\(row))"
    }
    
    var highlightedSpriteName: String {
        return spriteName + "-Highlighted"
    }
    
    static func random() -> CookieType {
        return CookieType(rawValue: Int(arc4random_uniform(6)) + 1)!
    }

}

```

Any help is greatly appreciated.

Thanks!`indent preformatted text by 4 spaces`

---

<div class="post-metadata">

**Author:** ![jj\_tagy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jj_tagy/32/102075_2.png) [@jj\_tagy](https://forums.kodeco.com/u/jj_tagy)\
**Post date:** [May 15, 2016, 9:10pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/11 "2016-05-15T21:10:45Z")

</div>

That’s because you closed out your CookieType enum. Those variables need to be inside.

---

<div class="post-metadata">

**Author:** ![banjochris](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@banjochris](https://forums.kodeco.com/u/banjochris)\
**Post date:** [May 16, 2016, 1:05am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/12 "2016-05-16T01:05:45Z")

</div>

Duh!!!

@jj_tagy Much appreciated!!! That, and a little closer inspection of my code vs the descriptions cured my errors to this point…

Thanks!!!

---

<div class="post-metadata">

**Author:** ![buckrussell](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/buckrussell/32/32757_2.png) [@buckrussell](https://forums.kodeco.com/u/buckrussell)\
**Post date:** [May 19, 2016, 6:41pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/13 "2016-05-19T18:41:08Z")

</div>

You really need to update this tutorial if you’re going to leave it on the website. A lot of the code is outdated and no longer works.

Also, you really need to write the functions (in the tutorial) BEFORE you reference them because beginners will get stuck trying to figure out why there is an error when your code references a function that doesn’t yet exist. They will not KNOW it’s a function and not some built in swift code. Clarity is best. Just sayin’.

As it stands now, this tutorial is useless to most people since about half way through your code creates an error that prevents moving further in the tutorial. Specifically when you state to add the following to the viewDidLoad method:

`scene.handleSwipe = swipeHandler`

this isn’t properly linking to the GameScene.swift file’s reference of swipeHandler. I’m certain it’s a difference in updated swift code (which again, sort of begs the question on why this tutorial isn’t updated if it’s still visible on the website).

Please fix this.

---

<div class="post-metadata">

**Author:** ![jj\_tagy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jj_tagy/32/102075_2.png) [@jj\_tagy](https://forums.kodeco.com/u/jj_tagy)\
**Post date:** [May 20, 2016, 12:33am UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/14 "2016-05-20T00:33:15Z")

</div>

Actually, I found that working through this tutorial and identifying the things that have changed in the newer Swift helped to understand the code much more than just copying what is there.

As for the swipeHandler piece, you need to do all pieces before it “connects”.

---

<div class="post-metadata">

**Author:** ![buckrussell](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/buckrussell/32/32757_2.png) [@buckrussell](https://forums.kodeco.com/u/buckrussell)\
**Post date:** [May 20, 2016, 12:40pm UTC](https://forums.kodeco.com/t/how-to-make-a-game-like-candy-crush-with-swift-tutorial-part-1/746/15 "2016-05-20T12:40:18Z")

</div>

of course. but that’s not the point. the tutorial is written very disjointedly. not a HUGE deal to those of us that already understand programming and can work our way through the errors but for beginners it pretty much guarantees they give up. that benefits no one.

i figured out the issue with the swipeHandler but again, a better written tutorial and that “error” would never have happened.

oh well.
