在使用SpriteNode在SKScene内购买应用程序?

时间:2015-08-09 15:30:32

标签: swift xcode6 sprite-kit in-app-purchase skscene

购买完成后,我正在使用此代码解锁一个场景。我的第一个问题是代码是否正确,购买是否有效。我的第二个问题是我要求在按下精灵节点时发生购买,我是否在触摸开始时调用了正确的函数?以下所有代码均在SKScene

    var list = [SKProduct]()
    var p = SKProduct()

override func didMoveToView(view: SKView) {

    if(SKPaymentQueue.canMakePayments()) {
        println("IAP is enabled, loading")
        var productID:NSSet = NSSet(objects: "_Scene1_Unlock")
        var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        request.delegate = self
        request.start() } else {
        println("please enable IAPS") }
}


    func addScene1() {
        println("scene1 added") }

    func RestorePurchases() {
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        SKPaymentQueue.defaultQueue().restoreCompletedTransactions() }

    func btnAddScene1() {
        for product in list {
            var prodID = product.productIdentifier
            if(prodID == "_Scene1_Unlock") {
                p = product
                buyProduct()
                break; } } }

    func buyProduct() {
        println("buy" + p.productIdentifier)
        var pay = SKPayment(product: p)
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment) }

    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
        println("product request")
        var myProduct = response.products
        for product in myProduct {
            println("product added")
            println(product.productIdentifier)
            println(product.localizedTitle)
            println(product.localizedDescription)
            println(product.price)
            list.append(product as! SKProduct) } }

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
        println("transactions restored")
        var purchasedItemIDS = []
        for transaction in queue.transactions {
            var t: SKPaymentTransaction = transaction as! SKPaymentTransaction
            let prodID = t.payment.productIdentifier as String
            switch prodID {
            case "_Scene1_Unlock":
                println("remove ads")
                addScene1()
            default:
                println("IAP not setup")}}}

    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
        println("add paymnet")
        for transaction:AnyObject in transactions {
        var trans = transaction as! SKPaymentTransaction
        println(trans.error)
        switch trans.transactionState {
        case .Purchased:
        println("buy, ok unlock iap here")
        println(p.productIdentifier)
        let prodID = p.productIdentifier as String
        switch prodID {
        case "_Scene1_Unlock":
        println("remove ads")
        addScene1()
        default:
        println("IAP not setup") }
        queue.finishTransaction(trans)
        break;
        case .Failed:
        println("buy error")
        queue.finishTransaction(trans)
        break;
        default:
        println("default")
        break; }}}

    func finishTransaction(trans:SKPaymentTransaction) {
        println("finish trans") }

    func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!) {
        println("remove trans") }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)

            if self.nodeAtPoint(location) == self.stopScene1 {
            btnAddScene1()
            }

0 个答案:

没有答案