输入" GameScene"不符合协议" SKPaymentTransactionObserver

时间:2016-06-08 18:39:57

标签: ios swift sprite-kit storekit

好的,所以我正在学习一个教程(http://stefansdevplayground.blogspot.com/2015/04/how-to-implement-in-app-purchase-for.html),当我完成它后,我遇到了错误:

  

Type" GameScene"不符合协议" 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

    }
}

1 个答案:

答案 0 :(得分:1)

每当您觉得自己符合协议时,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) {
    ...
}