使用Sprite Kit和iOS

时间:2016-06-22 21:47:55

标签: swift sprite-kit

我一直试图在我的游戏应用游戏结束时显示添加内容。我在GameScene.swift上创建了一个变量游戏,我在GameViewController.swift中调用了该变量。但是,在游戏开始后不久就会显示添加。谁能指出我做错了什么?这是我的代码:

import UIKit
import SpriteKit
import GoogleMobileAds

private var interstitial: GADInterstitial?

class GameViewController: UIViewController,  GADInterstitialDelegate {

    private var interstitial: GADInterstitial?

    override func viewDidLoad() {
        super.viewDidLoad()
        interstitial = createInterstitial()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.



            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill

            skView.presentScene(scene)
        }
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }


    private func createInterstitial() -> GADInterstitial {
        let interstitial = GADInterstitial(adUnitID: "ca-app-pub-5713504307801022/7610056592")
        interstitial.delegate = self
        let request = GADRequest()

        request.testDevices = ["ca-app-pub-5713504307801022/7610056592"]
        interstitial.loadRequest(request)
        return interstitial
    }

    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        interstitial = createInterstitial()
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!) {

        if (interstitial!.isReady)  {
            if gameOver1 == true {
            self.interstitial!.presentFromRootViewController(self)
            interstitial = createInterstitial()
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

首先 - 您应该如何加载和回收AdMob插页式广告 - https://stackoverflow.com/a/37938286/2405378

第二 - viewWillLayoutSubviews可以多次调用,每次此方法创建GameScene的新实例 - gameOver1为false?