2016-05-02 8 views
-1

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 

Antwort

1

hier Nun, die Sache: Sie setzen den bonusPointTotal auf 0 dann, wenn Sie wieder drücken Sie 0 hinzufügen, auf die normale Punktzahl (30).

Das Problem ist in Ihrem Streak Broken Code.

Zunächst ist die Einstellung der totalScore nutzlos, da Sie sie erneut einstellen, wenn Sie eine normale Taste drücken und sie nicht vor dem normalen Drücken der Taste verwenden.

Zweitens glaube ich, dass die Reparatur Ihres Codes ist nur eine Frage der Ersetzung der ersten Zeile Streak Broken. Ersetzen Sie also totalScore = (normalScore + bonusPointTotal) durch normalScore = totalScore.

Lassen Sie mich die Logik in Ihrem Fall erklären. Kurz vor dem Streak brechen Sie haben insgesamt von 33. Sie haben die Streifen brechen und setzen Sie den Bonus auf 0 Normalerweise (mit Code) der Gesamtscore niedriger sein wird (30) als zuvor (33). Aber wenn Sie die normale Partitur gesetzt zu Gesamtscore, wenn Sie die Streifen brechen Sie dann eine normalen Punktzahl von 33. Dann Sie die 3 von dem normalen Tastendruck hinzufügen und Sie erhalten insgesamt von 36 (mit Bonus 0 und normal 36).

Das ist es.

+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