类型 "GameScene" 不符合协议“SKPaymentTransactionObserver

Type "GameScene" does not conform to protocol "SKPaymentTransactionObserver

好的,所以我正在学习教程 (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

    }
}

每当您觉得自己遵守某个协议但 Xcode 告诉您您不遵守时,查看协议文档以了解您可能错在哪里是一个明智的想法。在这种情况下,协议 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) {
    ...
}