问题描述
我正在使用 Xcode 和 SpriteKit 制作游戏,我正在使用 admob 集成广告,并且所有代码都适用于横幅广告,并且能够让插页式广告也能正常工作.我的问题是告诉游戏何时显示插页式广告.广告在 GameViewController 中,但我需要从 GameScene.swift 调用 showAd() 方法我将如何在 GameScene 中从 GameViewController 调用函数.任何帮助表示赞赏,在此先感谢 Zach.
I am making a game using Xcode and SpriteKit, I am integrating ads using admob and have all of the code working for the banner ads and have been able to get the interstitial ads to work as well. My problem is telling the game when to display the interstitial ad. The ad is in GameViewController but I need to call the showAd() method from GameScene.swift How would I go about calling a function from GameViewController in GameScene. Any help is appreciated, thanks in advance, Zach.
推荐答案
在你的 GameViewController
中,像这样在 viewWillLayoutSubviews
中设置一个通知观察者:
In your GameViewController
, setup a notification observer in viewWillLayoutSubviews
like so:
override func viewWillLayoutSubviews() {
NotificationCenter.default.addObserver(self, selector: #selector(self.showAd), name: NSNotification.Name(rawValue: "showAd"), object: nil)
}
然后在您的 GameScene 中,当您希望 GameViewController
中的函数运行时调用它:
Then in your GameScene, call this when you want the function in GameViewController
to be run:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showAd"), object: nil)
请记住,您需要在 GameViewController
中调用一个函数.showAd
只是您希望在 GameViewController
中运行的任何功能的占位符.
Keep in mind that you need to have a function to be called inside your GameViewController
. showAd
is just a placeholder for whatever function you want to be run inside your GameViewController
.
希望这会有所帮助!
这篇关于需要帮助在 Spritekit iOS 游戏中集成插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!