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

时间:2015-03-21 13:41:17

标签: swift iad

我做错了什么?我试图让广告在广告被点击时暂停,但它仍然在后台运行。我怎么让它暂停?我尝试了其他方法来暂停场景,但它并不适合我。

    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-----------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

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

我认为你应该检查skView是否是你的游戏视图。 self.view可能是此时的广告视图。

要解决此问题,请找到您的观点,而不是使用自己。

//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
            }
        }
    }