2017-12-20 14 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. 使用インデント

私はそれが助けてくれることを願うお気軽に質問してください。

関連する問題