【问题标题】:swift: How to call an ad made in ViewController in game sceneswift:如何在游戏场景中调用 ViewController 中制作的广告
【发布时间】:2016-07-11 14:07:57
【问题描述】:

我在 GameViewController 中声明了一个插页式广告,我想每隔几次游戏结束就调用它。我遵循了一个教程,我可以通过一个按钮调用广告,但我需要从 GameScene 调用,但我不知道如何跨类调用。我可以在所有类之间使用全局变量吗? 基本上我想在 GameScene 中调用 func adButton() 或者可能有更好的解决方法。

另外,有没有办法实现奖励广告,或者有没有人有类似的好教程?

非常感谢。

func createAndLoadInterstitial() -> GADInterstitial {

    let interstitial = GADInterstitial(adUnitID: " unit id ")
    let request1 = GADRequest()
    interstitial.delegate = self
    request1.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9b" ]
    interstitial.loadRequest(request1)
    return interstitial




}


func adButton() {

    if (self.interstitial.isReady)
    {
        self.interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAndLoadInterstitial()
    }
}

【问题讨论】:

    标签: ios swift admob


    【解决方案1】:

    使用notifictionCenter调用函数

    在 viewDidLoad() 中添加第一个函数,在 adButton() 中添加第二个函数

    在 Swift 3 中

    //In the gameviewcontroller
        NotificationCenter.default.addObserver(self, selector: #selector(YourFunctionNameHere), name: "NSNotificationCreate" as NSNotification.Name, object: nil)
    //In the gameScene use this to call the ad
        NotificationCenter.default.post(name: "NSNotificationCreate" as NSNotification.Name, object: nil)
    

    在 Swift 2 中

    //In the gameviewcontroller
       NSNotificationCenter.defaultCenter().addObserver(self, selector: "YourFunctionNameHere", name: "NSNotificationCreate", object: nil)
    
    //In the gameScene use this to call the ad
       NSNotificationCenter.defaultCenter().postNotificationName("NSNotificationCreate", object: nil)
    

    【讨论】:

    • 工作得非常完美,非常感谢,我一直坚持这一点。这个通知中心是您在课程之间/整个课程中发送信息的一种方式吗?我不需要它,但它可以用于任何功能吗?再次感谢。
    • 您可以使用它来调用另一个 ViewController 或 GameScene @yellowlan 上的任何函数
    • 通知为我解决了这个问题。我也一直在努力寻找一种显示插页式广告的方法。这正是我所需要的。谢谢!
    猜你喜欢
    • 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
    相关资源
    最近更新 更多