【问题标题】:How have Ad Mob Ads only appear on certain scenes?Admob 广告如何只出现在某些场景中?
【发布时间】:2015-02-13 21:47:54
【问题描述】:

我有一款 Ad Mob Ads 游戏。我正在使用 SpriteKit。广告效果很好,但我只希望它们出现在特定场景中。例如让它们在游戏场景中消失。我的广告横幅设置如下。在我的 main.storyboard 中,我有一个 320x50 的 UIView,它连接到我的 GameViewController 中的 GADBannerView。一切都很好,但在每个场景中都有一个横幅广告。如何使横幅仅出现在主菜单场景和游戏结束场景中。这些是SKScenes。请帮忙!谢谢!

附言我正在使用 Swift

【问题讨论】:

    标签: swift sprite-kit admob


    【解决方案1】:

    这是实现它的一种方法:

    在你的 ViewController.swift 中,定义

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAd:", name: "gameStateOff", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideAd:", name: "gameStateOn", object: nil)
    

    使用这些在您的场景和视图控制器之间进行通信。

    接下来,实现 showAd 和 hideAd 函数,当您的场景进入或离开“游戏状态”时将调用它们:

    func showAd(notif: NSNotification) {
        // this func places the ad to the bottom of the screen
        var frame = ad.frame
        frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
        frame.origin.y = view.bounds.height - frame.size.height
        ad.frame = frame
      }
    
      func hideAd(notif: NSNotification) {
        // this func places the ad outside the screen
        var frame = ad.frame
        frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
        frame.origin.y = -frame.size.height
        ad.frame = frame
      }
    

    其中 ad 是您的 GADBannerView(adSize: kGADAdSizeBanner)。

    接下来将其添加到您开始游戏的位置

    NSNotificationCenter.defaultCenter().postNotificationName("gameStateOn", object: nil)
    

    此调用使视图控制器执行 hideAd 功能。

    当您想再次展示广告时,请致电

    NSNotificationCenter.defaultCenter().postNotificationName("gameStateOff", object: nil)
    

    【讨论】:

    • 这就像一个魅力!谢啦。太糟糕了,OP 没有将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多