0
卡片相等時分數會增加,這是我想要的,但卡片不相等時也會增加。我嘗試了使用不等號但仍然沒有任何運氣。不等號不能在swift中工作
@IBAction func SnapButtonTapped(sender: UIButton) {
if firstRandomNumber == SecondRandomNumber {
print("index match")
self.playerScore += 1
self.playerScoreLabel.text = String(self.playerScore)
}
else if firstRandomNumber != SecondRandomNumber {
print("no match")
}
//get the audioPlayer that correspond to the button that they tapped
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
這是我的代碼的其餘部分。
import UIKit
import AVFoundation
let firstRandomNumber = Int()
let SecondRandomNumber = Int()
let firstCardString = String()
let SecondCardString = String()
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!
let soundFilenames = ["shuffle", "correct"]
var audioPlayers = [AVAudioPlayer]()
var playerScore: Int = 0
var cardNamesArray: [String] = ["sad", "sleepy","angry"]
var cardNamesArray2: [String] = ["triste", "cansado","enfadado"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//set up audio player
for sound in soundFilenames {
do {
//try to do something
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType:"wav")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
audioPlayers.append(audioPlayer)
}
catch {
//catch the error
audioPlayers.append(AVAudioPlayer())
}
}
self.PlayRoundButton.setTitle("Play", forState: UIControlState.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playRoundTapped(sender: UIButton) {
self.PlayRoundButton.setTitle("Shuffle Cards", forState: UIControlState.Normal)
//First Card generate random number
let firstRandomNumber = Int (arc4random_uniform(3))
let firstCardString:String = self.cardNamesArray[firstRandomNumber]
self.FirstCardImageView.image = UIImage(named: firstCardString)
//Second Card generate random number
let SecondRandomNumber = Int (arc4random_uniform(3))
let SecondCardString:String = self.cardNamesArray2[SecondRandomNumber]
self.SecondCardImageView.image = UIImage(named: SecondCardString)
//determine matching cards
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
感謝您的工作 – Miguel
將其移至答案。如果有效,請隨時接受答案。 – Grimxn