首先让我为所有代码说抱歉....我想确保我包含大部分代码,这样你就可以看到我做错了什么。我的游戏有一个应用内购买解锁级别。当我去购买2级时,它可以很好地工作,但是当我退出应用程序并返回应用程序购买的2级时就消失了。为什么会这样?我不知道为什么它不保存应用内购买任何人都没有看到任何错误的代码。如果你不明白我说的话,请告诉我,我会尝试更好地解释它。谢谢!
编辑:
override func didMoveToView(view: SKView) {
if unlockLeavel2 {
let unlockLeavel2 = NSUserDefaults().boolForKey("Leavel2")
unlockLevelTwo()
}
}
func unlockLevelTwo() {
let fadeIn = SKAction.fadeInWithDuration(1.0)
levelTwo.position = CGPointMake(self.size.width / 2.0, self.size.height / 2.2)
levelTwo.zPosition = 20
levelTwo.setScale(0.8)
levelTwo.alpha = 0
levelTwo.hidden = false
levelTwo.runAction(fadeIn)
levelTwo.name = "leveltwo"
addChild(levelTwo)
}
func unlockLevel() {
if SKPaymentQueue.canMakePayments() {
var productID:NSSet = NSSet(object: "unlockLevelTwo")
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
productsRequest.delegate = self
productsRequest.start()
println("Fetching products...")
}
else {
println("Can't make purchases")
}
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
println("Product Purchased");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
NSUserDefaults().setBool(true, forKey: "Leavel2")
unlockLevelTwo()
break;
case .Failed:
println("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .Restored:
println("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
default:
break;
}
}
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch: UITouch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if node.name == "unlockleveltwobutton" {
if defaults.boolForKey("unlockthelevelTwo") == false {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
runAction(menuAudioPlayer)
unlockLevel()
}
}
答案 0 :(得分:1)
您的unlockLevelTwo()
功能正在调整当前会话中的游戏数据,并且数据不会在会话之间持续存在。
为了保存该状态,您可以使用一些主要NSCoding
,NSUserDefaults
或甚至CoreData
的功能来保存该游戏数据。
保存
let save = NSUserDefaults.standardUserDefaults()
save.setValue(yourVariable, forKey: "SomeUniqueKey")
save.synchronize() // makes ur data persist between sessions
加载
if let data = save.valueForKey("SomeUniqueKey") {
// some data here
}
else {
// got no data
}
答案 1 :(得分:0)
这里我设置了bool键&#34;已购买&#34;
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
println(trans)
switch trans.transactionState {
case .Purchased:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
defaults.setBool(true , forKey: "purchased")
loadingNotification.hide(true)
break;
case .Failed:
println("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
loadingNotification.hide(true)
break;
default:
break;
}
}
}
loadingNotification.hide(true, afterDelay: 45)
}
使用nsuerdefault删除这里的添加代码
override func viewDidLoad() {
super.viewDidLoad()
if (defaults.boolForKey("purchased"))
{
bannerAds.removeFromSuperview()
customView.hidden = false
println("Remove ads from 2")
}
else
{
// Display Ads code
}
}
我希望它有用!!!!!