In Swift, ich habe ein einfaches ‚Spiel‘, das eine Punktzahl basierend auf einer bestimmten Schaltfläche in der Benutzeroberfläche Tallys auf, die gedrückt wird. 15 normale Tasten. 1 "Streifen gebrochen" -Taste. Sobald der "Streak broken" -Knopf angetippt wird, sollte der Score den Punktestand mit dem zusätzlichen Streakbonus widerspiegeln.Swift - Logic für das Hinzufügen eines „Bonus streak“
Es funktioniert gut, bis eine normale Taste gedrückt wird. Dann wird meine Streak-Spur auf Null gesetzt und die Punktzahl wird bei der nächsten "normalen" Taste falsch addiert.
Hier ist mein log:
Streak counter: 1/Bonus point total: 0/Score total: 3/TOTAL SCORE: 3
Streak counter: 2/Bonus point total: 0/Score total: 6/TOTAL SCORE: 6
Streak counter: 3/Bonus point total: 0/Score total: 9/TOTAL SCORE: 9
Streak counter: 4/Bonus point total: 0/Score total: 12/TOTAL SCORE: 12
Streak counter: 5/Bonus point total: 0/Score total: 15/TOTAL SCORE: 15
Streak counter: 6/Bonus point total: 0/Score total: 18/TOTAL SCORE: 18
Streak counter: 7/Bonus point total: 2/Score total: 21/TOTAL SCORE: 23
Streak counter: 8/Bonus point total: 4/Score total: 24/TOTAL SCORE: 28
Streak counter: 9/Bonus point total: 6/Score total: 27/TOTAL SCORE: 33
STREAK BROKEN!
Streak counter: 1/Bonus point total: 0/Score total: 30/TOTAL SCORE: 30
Sie können die STREAK BROKEN sehen, dass einmal! Taste wird angetippt, die Noten addieren sich beim nächsten normalen Tastendruck nicht richtig. Das Gesamtergebnis sollte (für einen normalen Hahn/0 für streak Bonus +3.) 36, nicht 30.
Normale Tastencode gedrückt:
// Calculate the addition of a score:
normalScore += 3
// Calculate the streak:
streakCounter += 1
switch streakCounter {
case 1...6:
bonusPointTotal += 0
case 7...9:
bonusPointTotal += 2
case 10...12:
bonusPointTotal += 3
case 13...16:
bonusPointTotal += 4
default:
bonusPointTotal += 0
}
bonusLabelCounter.text = "+\(bonusPointTotal)"
totalScore = (normalScore + bonusPointTotal)
scoreLabelCounter.text = "\(totalScore)"
print("Streak counter: \(streakCounter)/Bonus point total: \(bonusPointTotal)/Score total: \(normalScore)/TOTAL SCORE: \(totalScore)")
STREAK BROKEN Taste gedrückt
totalScore = (normalScore + bonusPointTotal)
// Reset the streak:
streakCounter = 0
print("STREAK BROKEN!")
bonusPointTotal = 0
Vielen Dank dafür !! Die Lösung hat perfekt funktioniert, und deine Logik hat einen Sinn ergeben. Ich weiß nicht, wie ich das übersehen habe, aber leider habe ich es getan. Gut gemacht! – Joe