I am currently following the YouTube Match Card Game Tutorial and have got to video 8, where we code the card flipping. Although the cards flip as they should, some cards flip to show no image. Does anyone know how to fix this?
I have included images to show the problem.
Hi Josh,
A couple of things come to mind.
-
Make sure that your assets folder has all the cards numbered from card1 up to card13 with no card numbers missing in the series (probably not the issue).
-
Check your getCards() code in CardModel that generates the 8 pairs of cards.
Your getCards() function code should look something like this:
func getCards() -> [Card] {
// Declare an array to store numbers we have already generated.
var generatedNumbersArray = [Int]()
// Declare an array to store the generated cards
var generatedCardsArray = [Card]()
// Randomly generate pairs of cards
while generatedNumbersArray.count < 8 {
// Get a random number
let randomNumber = arc4random_uniform(13) + 1
// Ensure tha the random number isn't one that we already have.
if generatedNumbersArray.contains(Int(randomNumber)) == false {
// Log the random number to the Console
print(randomNumber)
// Store the numvber into the generatedNumbersArray
generatedNumbersArray.append(Int(randomNumber))
// Create the first card object
let cardOne = Card()
cardOne.imageName = "card\(randomNumber)"
// Add to generatedCardsArray
generatedCardsArray.append(cardOne)
// Create the second card object
let cardTwo = Card()
cardTwo.imageName = "card\(randomNumber)"
// Add to generatedCardsArray
generatedCardsArray.append(cardTwo)
}
// Make it so that we only have unique pairs of cards
}
// Randomize the array
for i in 0...generatedCardsArray.count - 1 {
// Find a random number to swap with
let randomNumber = Int(arc4random_uniform(UInt32(generatedCardsArray.count)))
// Swap the two cards
let temporaryStorage = generatedCardsArray[i]
generatedCardsArray[i] = generatedCardsArray[randomNumber]
generatedCardsArray[randomNumber] = temporaryStorage
}
// Return the array
return generatedCardsArray
}
Thanks for the reply!
I haven’t quite done as much as you have but I have very similar code for when I currently am in the tutorial, so the program should work as expected. I have copied in a picture of my code, as well as the assets and would really appreciate if you could point me in the direction of my error, as I cannot see it myself.
Hi Josh,
I think I can see the error.
There is an underscore character in front of the card name where I have placed the arrow.
If you remove the underscore then the code should work.
Thanks so much Chris, it all works now! I really appreciate the help.