当用户在Swift中购买游戏物品时,如何减去游戏中的硬币?

时间:2015-08-05 02:43:28

标签: ios swift

当用户购买物品时,我无法减去游戏中的总硬币。我有这个角色,花费250,我想减去我所拥有的总硬币250,但它不起作用我做错了什么?

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)

    var touch: UITouch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)

    if node.name == "buyItem" {
        var defaults=NSUserDefaults()
        var coinScore = defaults.integerForKey("coinScore")


        if coinScore >= 250 {

        //Im using this to subtract the coins from the total coins but it doesn't subtract when I press on the button. 

        coinScore - 250

        //These 4 lines are to unlock the character and get rid of the button

        NSUserDefaults().setBool(true, forKey: "unlock250")
        yellowImage.removeFromParent()
        unlockYellowImage()
        yellowButton.removeFromParent()

        }

    }

 }

2 个答案:

答案 0 :(得分:2)

coinScore - 250只是计算减法,但结果不会在任何地方分配。您需要执行coinScore = coinScore - 250,可以将其写为coinScore -= 250

此外,如果您希望下次执行defaults时更新的值,则需要将其存储在defaults.integerForKey("coinScore")中。使用defaults.setInteger(coinScore, forKey: "coinScore")

您似乎也应该使用NSUserDefaults.standardUserDefaults()代替NSUserDefaults()

答案 1 :(得分:0)

你必须编写一个自定义函数,只要你想从你的UserDefaults数据中减去硬币,我就一直在搜索这样的东西,当我知道如何做到这一点我写了自己的自定义函数为它

这也适用于Swift 3

var unlocked = UserDefaults().bool(forKey: "Unlocked")
var price = 0

func PurchaseBtnClicked() {
    func deductCoins() -> Bool {

        if price > coins {
            print("not enough coins")
            return false
        }
        if coins > price {
            coins = coins - price
            UserDefaults.standard.set(coins, forKey: "Coins")
            UserDefaults.standard.set(true, forKey: "Unlocked")
            return true
        }

        return true
    }
    price = 250 //make sure to set the price here before the deductCoins() function is called otherwise you may over/underprice the user
    deductCoins()
    crowBackBtn.removeFromSuperview()
    crowPurchaseBtn.removeFromSuperview()
}
func BackBtnClicked() {
    crowBackBtn.removeFromSuperview()
    crowPurchaseBtn.removeFromSuperview()
}
if UserDefaults().bool(forKey "Unlocked") == true {//this is called when the boolean value unlocked is saved as true
    print("content is already unlocked")
    //here I want to push the viewController to another viewController so the user can view the content he/she has bought
} else {
    //in here is where I will add my prompt label and a button to confirm the purchase 
        PurchaseBtn.setImage(UIImage(named: "yesImage"), for: .normal)
        PurchaseBtn.frame = CGRect(x: view.frame.size.width / 3, y: view.frame.size.height / 2, width: 90, height: 30)
        PurchaseBtn.addTarget(self, action:#selector(self.PurchaseBtnClicked), for: .touchUpInside)
        BackBtn.setImage(UIImage(named: "noImage"), for: .normal)
        BackBtn.frame = CGRect(x: view.frame.size.width / 3, y: view.frame.size.height / 1.7, width: 90, height: 30)
        BackBtn.addTarget(self, action:#selector(self.BackBtnClicked), for: .touchUpInside)
        self.view.addSubview(PurchaseBtn)
        self.view.addSubview(BackBtn)    
}

你应该能够复制并粘贴上面的代码,但一定要更改它只能工作一次的图像,因为一旦按下PurchaseBtn就会购买该项目

<强> - 编辑 -

哦拍我刚才在回顾这个时就意识到我发布了这个意味着一个viewController而不是GameScene