- Add dynamic current score display

- Fix minimum score setting
This commit is contained in:
CypherPoet 2019-01-14 23:37:09 -05:00
parent d3018c2837
commit ec982d1ae3
2 changed files with 21 additions and 3 deletions

View File

@ -49,6 +49,13 @@
<action selector="buttonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="U4R-CP-ARt"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Current Score" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4XS-g3-zAP">
<rect key="frame" x="72" y="503" width="230" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" weight="heavy" pointSize="24"/>
<color key="textColor" red="0.36238711552087849" green="0.35869689454197995" blue="0.36653315355329952" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
@ -66,6 +73,7 @@
<outlet property="button1" destination="vFN-ge-cVu" id="tFe-hn-j0d"/>
<outlet property="button2" destination="ZzW-Kt-38n" id="gbi-UL-J0u"/>
<outlet property="button3" destination="NEb-PX-xnZ" id="FBe-Yt-iKY"/>
<outlet property="scoreLabel" destination="4XS-g3-zAP" id="vVY-uo-ddQ"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

View File

@ -28,8 +28,14 @@ class ViewController: UIViewController {
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var scoreLabel: UILabel!
var currentScore = 0 {
didSet {
scoreLabel.text = "Current Score: \(self.currentScore)"
}
}
var currentScore = 0
var flagChoiceKeys = [String]()
var correctFlagKey: String?
@ -37,6 +43,7 @@ class ViewController: UIViewController {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
currentScore = 0
setupButtonStyles()
askQuestion()
}
@ -68,15 +75,18 @@ class ViewController: UIViewController {
func handleChoice(wasCorrect: Bool) {
var responseMessage: String
if wasCorrect {
title = "Correct!"
responseMessage = "You just gained 1 point."
currentScore += 1
} else {
title = "Incorrect!"
currentScore = min(currentScore - 3, 0)
responseMessage = "You just lost 3 points"
currentScore = max(0, currentScore - 3)
}
let responseMessage = "Your score is now \(currentScore)."
let alertController = UIAlertController(title: title, message: responseMessage, preferredStyle: .alert)
alertController.addAction(