I'm making this card game and need some bit of help, I'm trying to shuffle the cards when they are matched. I'm trying to call the ibaction method where it does all the shuffling inside another ibaction so that when the cards match they shuffle when the user clicks on "snap". It doesn't 100% work, cards that are not the same gets matched and only one card gets shuffled.
import GameplayKit
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var FirstCardImageView: UIImageView!
@IBOutlet weak var SecondCardImageView: UIImageView!
@IBOutlet weak var PlayRoundButton: UIButton!
@IBOutlet weak var BackgroundImageView: UIImageView!
@IBOutlet weak var playerScoreLabel: UILabel!
var cardNamesArray = [String]()
var cardNamesArray2 = [String]()
var playerScore: Int = 0
var firstRandomNumber = Int()
var SecondRandomNumber = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cardNamesArray += ["acorn", "angry","apple", "rainbow", "sad","boots", "heart", "pumpkin","chestnuts"]
cardNamesArray2 += ["bellota", "enfadado","manzana", "arcoiris","triste","botas","corazon", "calabaza", "castana"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
This is the method that creates the cards to be random but not completely sure if it's the correct way.
@IBAction func playRoundTapped(sender: UIButton) {
cardNamesArray = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(cardNamesArray) as! [String]
cardNamesArray2 = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(cardNamesArray2) as! [String]
let firstCardString:String = self.cardNamesArray[firstRandomNumber] <---- Not sure if this is the correct way
self.FirstCardImageView.image = UIImage(named: firstCardString)
let SecondCardString:String = self.cardNamesArray2[SecondRandomNumber]
self.SecondCardImageView.image = UIImage(named: SecondCardString)
This is where I want it to generate a random card when the user finds a matching pair. when I use this part of code 'self.playRoundTapped(self.PlayRoundButton)' cards match when they are not even and only one card view gets shuffled.
}
//snap button
@IBAction func SnapButtonTapped(sender: UIButton) {
if firstRandomNumber == SecondRandomNumber {
self.playRoundTapped(self.PlayRoundButton)//<---------- Problem is here
print("index match")
self.playerScore += 1
self.playerScoreLabel.text = String(self.playerScore)
cardNamesArray.removeAtIndex(firstRandomNumber)
cardNamesArray2.removeAtIndex(SecondRandomNumber)
if cardNamesArray.count == 0 && cardNamesArray2.count == 0{
}
}
else if firstRandomNumber != SecondRandomNumber {
print("no match")
}
}
}
Aucun commentaire:
Enregistrer un commentaire