2017-06-10 6 views
0

私はこのボタンを押すとスコアが1つ上がり、50を超えるとボタンを押すとスコアに2が加算されます。しかし問題は、すべてのボタンを押すごとにレベルが変わるということです。ここに私のコードは次のとおりです。変数が上がらない場合

import UIKit 

class ViewController: UIViewController { 

// OUTLETS 

@IBOutlet weak var score: UILabel! 
@IBOutlet weak var levelLabel: UILabel! 
@IBAction func add(_ sender: Any) { 
    add() 
} 


// VARIABLES 

var scoreVar = 0 
let levelUpAt = [10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000] 
var currentLevel = 1 
var toAdd = 1 

// FUNCTIONS 

// Below code adds to the score 

func add() { 
    if (scoreVar - 1 < levelUpAt[currentLevel - 1] && levelUpAt.indices.contains(currentLevel)) { // Complicated math-y if statment THANK YOU RASHWAN L (STACK OVERFLOW) FOR HELPING 
     currentLevel += 1 // Change level 
     toAdd += 1 // Change toAdd 
     levelLabel.text = "Level \(currentLevel)" // Change the level label 
     scoreVar += toAdd // Adds toAdd (level amount) to scoreVar 
     score.text = "\(scoreVar)"; // Updates text to match 
    } else { 
     scoreVar += toAdd // Adds toAdd (level amount) to scoreVar 
     score.text = "\(scoreVar)"; // Updates text to match 
    } 
} 
} 
+0

あなたは1を追加しているだけなので、2になるのはなぜですか?何か不足していますか? – LinusGeffarth

+0

ポイントは「レベル」を持つため、10,50,100などに達すると、それは 'toAdd'に1を加えます。つまり' scoreVar + = toAdd'が実行され、 'toAdd'が2 'scoreVar'に2を加えます。 –

答えて

0

if条件はelseコードおよびその逆を実行することになっているように見えます。そして、比較する最初の値は、scoreVar - 1ではなく、scoreVarです。

func add() { 
    if (scoreVar < levelUpAt[currentLevel - 1] && levelUpAt.indices.contains(currentLevel)) { // Complicated math-y if statment THANK YOU RASHWAN L (STACK OVERFLOW) FOR HELPING 
     scoreVar += toAdd // Adds toAdd (level amount) to scoreVar 
     score.text = "\(scoreVar)"; // Updates text to match 
    } else { 
     currentLevel += 1 // Change level 
     toAdd += 1 // Change toAdd 
     levelLabel.text = "Level \(currentLevel)" // Change the level label 
     scoreVar += toAdd // Adds toAdd (level amount) to scoreVar 
     score.text = "\(scoreVar)"; // Updates text to match 
    } 
}