2017-12-20 7 views
1

저는 현재 클릭/돈 게임을 만들고 있는데 도움이 필요합니다. 기본적으로 앱은 클릭 할 때마다 +1 동전을주는 버튼으로 구성됩니다. 예를 들어 "10 동전을위한 이중 동전"을 구입하면 금액을 +1에서 +2로 변경하고 싶습니다. Xcode UIbutton 함수

Click here to see how the app looks right now, it might be easier to understand.

에서

는 관련이있을 수있는 몇 가지 코드입니다.

@IBAction func button(_ sender: UIButton) { 
    score += 1 
    label.text = "Coins: \(score)" 
    errorLabel.text = "" 
    func doublee(sender:UIButton) { 
     score += 2 
    } 


    @IBAction func points(_ sender: UIButton) { 

    if score >= 10 { 
    score -= 10 
    label.text = "Coins: \(score)" 
    doublePoints.isEnabled = false 
    xLabel.text = "2X" 
    xLabel.textColor = UIColor.blue 
    } else { 
     errorLabel.text = "ERROR, NOT ENOUGH MONEY" 
    } 

프로그래밍을 시작한 후 모든 의견을 환영합니다. 고맙습니다!

+0

어떤 문제가 있습니까? –

답변

1

당신이 버튼을 누를 때 얻을, 당신은 점수 배율을 구매할 때,과 같이, 그에 따라 변수를 증가 얼마나 많은 포인트를 추적 변수 추가 :

var scoreIncrease = 1 // this is how much the score increases when you tap 

// This is called when the "CLICK HERE" button is tapped 
@IBAction func button(_ sender: UIButton) { 
    score += scoreIncrease 
    label.text = "Coins: \(score)" 
    errorLabel.text = "" 
} 

// This is called when you buy the 2x 
@IBAction func points(_ sender: UIButton) { 

    if score >= 10 { 
    score -= 10 
    label.text = "Coins: \(score)" 
    doublePoints.isEnabled = false 
    xLabel.text = "2X" 
    xLabel.textColor = UIColor.blue 
    scoreIncrease *= 2 // increase by x2 
    } else { 
    errorLabel.text = "ERROR, NOT ENOUGH MONEY" 
    } 
} 
+0

감사합니다. 작동했습니다! :) –

0

을 나는 당신의 문제를 이해하는 경우를 정확하게, 전반적인 점수에 추가 할 필요가있는 클릭당 동전의 가치를 유지하기위한 상태 변수가 필요합니다.

당신은 이런 식으로 작업을 수행 할 수 있습니다 코드에

class GameViewController: UIViewController { 
    // this is your state variables 
    var coinsPerClick = 1 
    var score = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     label.text = "Coins: \(score)" 
     errorLabel.text = .empty 
    } 

    @IBAction func getCoins(_ sender: UIButton) { 
     score += coinsPerClick 
     label.text = "Coins: \(score)" 
     // you can clear error label here 
     errorLabel.text = .empty 
    } 

    @IBAction func doubleCoinsPerClick(_ sender: UIButton) { 
     guard canUpgrade() else { 
     errorLabel.text = "ERROR, NOT ENOUGH MONEY" 
     return 
     } 

     doublePoints.isEnabled = false 
     score -= 10 
     coinsPerClick *= 2 
     label.text = "Coins: \(score)" 
    } 

    private func canUpgrade() -> Bool { 
     return score >= 10 && doublePoints.isEnabled 
    } 
    } 

몇몇 발언 : 코드의 시각적 별도의 블록에

  1. 을 사용하여 설명하는 이름
  2. 사용 들여 쓰기

나는 그것이 도움이되기를 바랍니다. 더 많은 질문을 주시기 바랍니다.

관련 문제