【发布时间】:2016-05-26 08:19:54
【问题描述】:
这很长,但它可能很容易解决。 大部分是代码,直接跳到底部就会有一个tl;dr
嘿,我已经学习了大约 10 个关于让 Admob 插页式广告在我的 Swift Spritekit 游戏中工作的教程。除了我见过的所有教程都使用带有按钮的 IBAction。 目前我希望广告在菜单上每隔一段时间弹出一次。 (刚开始,我以后可能会改变它)。 这一切都很完美,但是一旦我按下播放按钮(进入不同的场景)然后返回菜单场景。 (GameScene) 插页式功能不再发生。没有错误,只是在应用程序完全关闭并重新打开之前不再执行此操作。
这是代码。
在我的 GameViewController 类中,我有:
var interstital: GADInterstitial!
在 GameViewController 中确实加载了我:
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as? SKView
skView!.ignoresSiblingOrder = false
scene.scaleMode = .AspectFill
scene.viewController = self
skView!.presentScene(scene)
interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687746813194/9687097060")
let request = GADRequest()
interstital.loadRequest(request)
在 GameViewController 类中我也有这些功能:
func ShowAd() {
print("doing the showadfunc now")
if (interstital.isReady) {
print("there is an inter ready")
interstital.presentFromRootViewController(self)
interstital = CreateAd()
}
else{
print("The interstitial wasn't ready.")
}
}
func CreateAd() -> GADInterstitial {
let interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687748683194/9687247060")
interstital.loadRequest(GADRequest())
return interstital
}
在我的 GameScene 的(菜单)类中:
var viewController: GameViewController!
在我的 GameScene 课程中,我有这个用于显示插页式广告的功能。
func adThing(){
//viewController?.ShowAd()
print("starting the ad function")
let waitForInterstitial = SKAction.waitForDuration(15.0)
let waitForInterstitialShort = SKAction.waitForDuration(3.0)
let showInterstitialAd = SKAction.runBlock({self.viewController?.ShowAd(); print("the function has been called..")})
let waitThenShowInterstitial = SKAction.sequence([showInterstitialAd,waitForInterstitial])
let waitThenShowInterStitialForever = SKAction.repeatActionForever(waitThenShowInterstitial)
//self.runAction(waitThenShowInterStitialForever)
self.runAction(waitForInterstitialShort, completion: {
print("its waited 3 seconds and will now start the ad thingo")
self.runAction(waitThenShowInterStitialForever)
})
print("done")
}
所以,我每 20 秒在我的 GameScene 中调用 ShowAd 函数(位于 gameviewcontroller 中),(我已经确认它至少认为它正在调用该函数。)该应用程序首先打开。但是当我更改场景然后返回菜单场景 (GameScene) 时,我的 GameScene 函数中的打印不断发生,我的代码 认为 它正在调用 ShowAd 函数,但似乎什么都没有发生这种情况时,不会弹出任何插页式广告,甚至 GameViewController 中 ShowAd 函数中的打印也不会弹出。
所以在我改变场景并返回之后,我的游戏似乎忘记了 GameViewController 是什么。如果我指的是没有“?”的 GameViewController它会使我的游戏崩溃,并显示错误“在展开可选选项时意外发现 nil”。那么换个场景之后,也许 GameViewController 就变成了 nil 了?我该如何解决这个问题。
请帮帮我!!! (如果你不知道,我是编程新手。我知道对我的代码发表讽刺评论一定很诱人,它真的很简单,目前还不是很好。)
如果你能帮助我,非常感谢,哇.. 感谢您阅读所有这些.. ;)
【问题讨论】:
标签: ios swift null admob interstitial