lundi 28 mars 2016

Swift 2: Index out of range

I'm creating a snap game with two array images, when the player matches the last element of the array I get error "Index out of range". How can I prevent this? maybe have a message that says "all cards have been matched" Thanks in advance

My card Arrays:

    var cardArray = ["angry", "apple", "boots","heart", "pumpkin", "rainbow", "sad", "acorn", "chestnuts"]
    var cardArray2 = ["enfadadoTxt", "manzanaTxt", "botasTxt", "corazonTxt", "calabazaTxt", "arcoirisTxt", "tristeTxt", "bellotaTxt", "castanaTxt"]

This is where is crashes:

@IBAction func snapButtonTapped(sender: UIButton) {

    if cardArray.count > 0 {

        if firstRandomNumber == secondRandomNumber {

            self.playerScore += 1
            self.playerScoreLabel.text = String(self.playerScore)

            cardArray.removeAtIndex(firstRandomNumber)
            cardArray2.removeAtIndex(secondRandomNumber)

            firstRandomNumber = getRandomIntFromArray(cardArray)
            secondRandomNumber = getRandomIntFromArray(cardArray2)

            firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])//The problem might be here
            secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])

            print(cardArray)
            print(cardArray2)


        } else {

            animationView.startCanvasAnimation()
            print("no match")


        }

    }

}




Aucun commentaire:

Enregistrer un commentaire