AdMob在SKScene中奖励video
我正在尝试在我的精灵套件游戏中创建一个奖励video广告。 我想在我的GameScene.swift类中添加奖励video,但问题是这不是ViewController
而是SKScene
。
这不起作用,因为我的GameScene.swift不是ViewController
:
ad.present(fromRootViewController: self)
我尝试了很多东西,但没有任何效果。 有人能帮助我吗? 谢谢!
(我希望在玩家死亡时显示此奖励广告。)
在GameViewController
,在viewWillLayoutSubviews
设置观察者:
override func viewWillLayoutSubviews() { NotificationCenter.default.addObserver(self, selector: #selector(self.startVideoAd), name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil) } func startVideoAd() { // Do something - play video ad }
在这种情况下,每当调用此通知时,将运行名为startVideoAd
内的函数。 显然,您希望将名称更改为要运行的函数名称。
然后,在你的GameScene
,要发送通知,你可以在任何地方/每当你想在GameViewController
运行该函数时运行它:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil)
希望这可以帮助!