gpt4 book ai didi

ios - 类型 "GameScene"不符合协议(protocol)“SKPaymentTransactionObserver

转载 作者:行者123 更新时间:2023-11-29 00:54:48 25 4
gpt4 key购买 nike

好吧,我正在遵循教程( http://stefansdevplayground.blogspot.com/2015/04/how-to-implement-in-app-purchase-for.html ),当我完成遵循它时,我遇到了错误:

Type "GameScene" does not conform to protocol "SKPaymentTransactionObserver".

我是一个初学者,以前没有使用过 storekit,但是一些帮助会很好。

(顺便说一句,我还没有将他在教程中创建的 NSUserDefaults 和名称更新到我的游戏中)

class RemoveAdsScene: SKScene, SKPaymentTransactionObserver, SKProductsRequestDelegate {

private var request : SKProductsRequest!
private var products : [SKProduct] = [] // List of available purchases
private var greenShipPurchased = false // Used to enable/disable the 'green ship'




let settingsTitle = SKLabelNode(text: "[REMOVE|ADS]")
let infoLabel = SKLabelNode(text: "Pay What You Want")
let infoLabel2 = SKLabelNode(text: "To Remove Ads!")
let button199 = SKSpriteNode(imageNamed: "199Button")

let backButton = SKSpriteNode(imageNamed: "BackButton")
var audioPlayer: AVAudioPlayer!

override func didMoveToView(view: SKView) {



infoLabel.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.825)
infoLabel.zPosition = 500
infoLabel.fontSize = 25
infoLabel.fontName = "Montserrat-Bold"
infoLabel.fontColor = SKColor.blackColor()
self.addChild(infoLabel)

infoLabel2.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.775)
infoLabel2.zPosition = 500
infoLabel2.fontSize = 25
infoLabel2.fontName = "Montserrat-Bold"
infoLabel2.fontColor = SKColor.blackColor()
self.addChild(infoLabel2)



settingsTitle.fontColor = UIColor.init(red: 0.902, green: 0.251, blue: 0.282, alpha: 1)
settingsTitle.fontSize = 85
settingsTitle.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.9)
settingsTitle.fontName = "KGDefyingGravityBounce"
self.addChild(settingsTitle)

button199.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
button199.zPosition = 15
self.addChild(button199)




backButton.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 0.3)
backButton.zPosition = 15
self.addChild(backButton)










if NSUserDefaults.standardUserDefaults().objectForKey("timeOfTheDay") as! String == "morning" {
backgroundColor = GlobalData.dayColor

infoLabel.fontColor = SKColor.blackColor()
infoLabel2.fontColor = SKColor.blackColor()
}
else {

backgroundColor = GlobalData.nightColor

infoLabel.fontColor = SKColor.whiteColor()
infoLabel2.fontColor = SKColor.whiteColor()


}





}






func inAppPurchase() {
let alert = UIAlertController(title: "In App Purchases", message: "", preferredStyle: UIAlertControllerStyle.Alert)

// Add an alert action for each available product
for (var i = 0; i < products.count; i += 1) {
let currentProduct = products[i]
if !(currentProduct.productIdentifier == "MySecondGameGreenShip" && greenShipPurchased) {
// Get the localized price
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.locale = currentProduct.priceLocale
// Add the alert action
alert.addAction(UIAlertAction(title: currentProduct.localizedTitle + " " + numberFormatter.stringFromNumber(currentProduct.price)!, style: UIAlertActionStyle.Default) { _ in
// Perform the purchase
self.buyProduct(currentProduct)

})
}
}
// Offer the restore option only if purchase info is not available
if(greenShipPurchased == false) {
alert.addAction(UIAlertAction(title: "Restore", style: UIAlertActionStyle.Default) { _ in
self.restorePurchasedProducts()

})
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default) { _ in

})
// Show the alert
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
// Initialize the App Purchases
func initInAppPurchases() {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
// Get the list of possible purchases
if self.request == nil {
self.request = SKProductsRequest(productIdentifiers: Set(["MySecondGameGreenShip","MySecondGameDonate"]))
self.request.delegate = self
self.request.start()
}
}





func buyProduct(product: SKProduct) {
let payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment)
}

func restorePurchasedProducts() {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
self.products = response.products
self.request = nil
}

func request(request: SKRequest, didFailWithError error: NSError) {
print(error)
self.request = nil
}

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction in transactions as! [SKPaymentTransaction] {
switch (transaction.transactionState) {
case .Purchased:
if transaction.payment.productIdentifier == "MySecondGameGreenShip" {
handleGreenShipPurchased()
}
queue.finishTransaction(transaction)
case .Restored:
if transaction.payment.productIdentifier == "MySecondGameGreenShip" {
handleGreenShipPurchased()
}
queue.finishTransaction(transaction)
case .Failed:
print("Payment Error: %@", transaction.error)
queue.finishTransaction(transaction)
default:
print("Transaction State: %@", transaction.transactionState)
}
}
}



func handleGreenShipPurchased() {
greenShipPurchased = true
checkAndActivateGreenShip()
// persist the purchase locally
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "MySecondGameGreenShip")
}

func checkAndActivateGreenShip() {
if NSUserDefaults.standardUserDefaults().boolForKey("MySecondGameGreenShip") {
greenShipPurchased = true

}
}

最佳答案

每当您觉得自己符合协议(protocol)但 Xcode 告诉您不符合时,明智的做法是查看协议(protocol)文档,看看哪里可能出错。在这种情况下,协议(protocol) SKPaymentTransactionObserver有一个必需的方法:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])

尝试实现该方法但失败,因为您的代码与函数定义不匹配。你写了

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {

要修复它,只需采用正确的函数头并稍微更改以下代码:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
...
}

关于ios - 类型 "GameScene"不符合协议(protocol)“SKPaymentTransactionObserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37710391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com