为什么在 Swift 中点击 iAd 时我的游戏不会暂停?

Why won't my game pause when an iAd is tapped on in Swift?

我做错了什么?我试图让游戏在点击广告时暂停,但它仍在后台运行。我如何让它暂停?我尝试了其他暂停场景的方法,但它对我不起作用。

    func didLoadAd(banner: ADBannerView) {
        adBannerView.hidden = false
    }


    func bannerViewWillLoadAd(banner: ADBannerView!) {


        println("Ad about to load")

    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {

        adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - view.bounds.size.height + adBannerView.frame.size.height / 2)

        adBannerView.hidden = false
        println("Displaying the Ad")

    }

    func bannerViewActionDidFinish(banner: ADBannerView!) {
        /* un-pause when ad is closed */
        if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            let skView = self.view as SKView
            skView.paused = false
            println("Close the Ad")
        }

    }

    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
       if  let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            let skView = self.view as SKView
            skView.paused = true
            println("Leave the application to the Ad")
        //pause game here
        }

        return true
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {

        //move off bounds when add didn't load

        adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height + view.bounds.size.height)
        adBannerView.hidden = true
        println("Ad is not available")

    }
//iAd-----------------------------------------------------------------------------------------------------

我认为你的问题出在这里:

let skView = self.view as SKView
skView.paused = true

而且我认为你应该检查 skView 是否是你的游戏视图。 self.view 可能是当时的广告浏览量。

要修复它,请找到您的视图,而不是使用 self。

//at top of class
var window:UIWindow?

// inside my method/function
if let viewControllers = window?.rootViewController?.childViewControllers {
    for viewController in viewControllers {
        if viewController.isKindOfClass(MyViewControllerClass) {
            println("Found it!!!")
            viewController.paused = true
            }
        }
    }