jeudi 21 avril 2016

Swift 2 - How can I have my card arrays to never match in viewDidLoad

Anyone know how I can have the first Image never match the second image in viewDidLoad(). I'm making a snap game where the firstimage has to match the second image when both are equal. But I don't want the first two cards to match as they are only the cover for the cards.

This code in viewDidLoad kind of works but eventually is will find the same two random numbers and match the cards when the game loads.

 override func viewDidLoad() {

        firstRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count - 1)
        firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])

        secondRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray2.count - 1)
        secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])
}

This is the code for the snap button when both cards are equal. This code is fine.

    @IBAction func snapButtonTapped(sender: UIButton) {
    if cardArray.count > 0 {
        if firstRandomNumber == secondRandomNumber {
            //player score
            self.playerScore += 1
            self.playerScoreLabel.text = String(self.playerScore)

            //if they match remove cards from the array
            cardArray.removeAtIndex(firstRandomNumber)
            cardArray2.removeAtIndex(secondRandomNumber)

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

            if(cardArray.count == 0) {

                return
            }

            //if cards match change to random card
            firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])
            secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])

        } else { 

            animationView.startCanvasAnimation() 
        } 
    } 




Aucun commentaire:

Enregistrer un commentaire