Hello there! I’ve just completed the portion of the Bull’s Eye tutorial where we’ve added 100 additional points for a perfect score and 50 points for a 99… the score on the alert menu is correct. For instance if I get a perfect score it tells me I’ve earned 200 points but that is not actually translating into the overall score, it’s still only adding on 100 points. Same if I hit 99… it says I’ve earned 149 points in the alert menu but only tallies 99 points to my score. Thank you in advance!
hi @hunterfitch,
It seems that your score is being added twice, if you post the code - you can get some suggestions on where and how to fix it.
cheers,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var targetLabel: UILabel!
@IBOutlet weak var scoreTotal: UILabel!
@IBOutlet weak var roundLabel: UILabel!
var currentValue = 0
var targetValue = 0
var score = 0
var round = 0
override func viewDidLoad() {
super.viewDidLoad()
let roundedValue = slider.value.rounded()
currentValue = Int(roundedValue)
startOverButton()
}
@IBAction func showAlert(_ sender: Any) {
let difference = abs(currentValue - targetValue)
var points = 100 - difference
score += points
let title: String
if difference == 0 {
title = "Perfect! 100 Bonus Points!"
points += 100
} else if difference < 5 {
title = "You Almost Had It!"
if difference == 1 {
points += 50
}
} else if difference < 10 {
title = "Pretty Good!"
} else {
title = "Not Even Close!"
}
let message = "You Scored \(points) Points"
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: {
action in
self.startNewRound()
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
@IBAction func sliderMoved(_ slider: UISlider) {
let roundedValue = slider.value.rounded()
currentValue = Int(roundedValue)
}
func startNewRound() {
round += 1
targetValue = Int.random(in: 1 ... 100)
currentValue = 50
slider.value = Float(currentValue)
updateLabels()
}
func updateLabels() {
targetLabel.text = String(targetValue)
scoreTotal.text = String(score)
roundLabel.text = String(round)
}
@IBAction func startOverButton() {
score = 0
round = 0
startNewRound()
}
}
You’ll have to put :
score += points
Below :
…
} else {
title = “Not Even Close!”
}score += points // Here
The title/message would get set for your alert, but score itself only gets the bare points added, without the extra points ( before the block that conditionally assigns extra points ) :]
Putting that line after the extra points block should result into a correct score as well, including the extra points.
Grts,
1 Like
Thank you! That worked and it makes sense!
1 Like
This topic was automatically closed after 166 days. New replies are no longer allowed.