swift: 如何在游戏场景中调用ViewController制作的广告
swift: How to call an ad made in ViewController in game scene
我在 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()
}
}
使用通知中心调用函数
在 viewDidLoad() 中添加第一个函数,在 adButton() 中添加第二个函数
在Swift3
//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)
我在 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()
}
}
使用通知中心调用函数
在 viewDidLoad() 中添加第一个函数,在 adButton() 中添加第二个函数
在Swift3
//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)