I would like to display Google Ads in my Sprite-Kit game. In the GameViewController I create the banner ad:
override func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
let skView = self.view as! SKView
googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
googleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
googleBannerView.rootViewController = self
var request: GADRequest = GADRequest()
googleBannerView.loadRequest(request)
googleBannerView.frame = CGRectMake(0, skView.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)
self.view.addSubview(googleBannerView!)
println(googleBannerView)
if skView.scene == nil{
let mainMenuScene = MainMenuScene(size: skView.bounds.size)
mainMenuScene.scaleMode = SKSceneScaleMode.AspectFill
mainMenuScene.backgroundColor = UIColor.whiteColor()
skView.presentScene(mainMenuScene)
}
}This works fine except for when I switch to the GameScene. I would like the ad to be hidden. Then in the GameOverScene I would like the add to be shown again. The problem is that the ad is equal to nil for some reason when I switch scenes, and it stays equal to nil. Here are the hideBanner() and showBanner() functions that don't work because it is equal to nil after the game switches scenes.
The hideBanner() function in the GameViewController:
func hideBanner(){
if googleBannerView != nil{
println("hideBanner() called")
self.googleBannerView.hidden = true
}
}The showBanner() function in the GameViewController:
func showBanner(){
if googleBannerView != nil{
self.googleBannerView.hidden = false
var request: GADRequest = GADRequest()
self.googleBannerView.loadRequest(request)
}
}The problem is as soon as it switches scenes the banner view is equal to nil, yet I can still see the banner; it hasn't changed.
In the the GameScene:
override init(size: CGSize) {
super.init(size: size)
let gameViewController = GameViewController()
println(gameViewController.googleBannerView)
gameViewController.hideBanner()And then the console prints out nil. I don't know why when I switch scene the ad banner becomes nil. In the MainMenuScene where the banner is created it prints out:
<GADBannerView: 0x7fe982715050; frame = (0 617; 375 50); clipsToBounds = YES; layer = <CALayer: 0x7fe982400fd0>>After that it seems to always be equal to nil, even when I come back to the MainMenuScene. I just can't figure it out. Does anyone know what to do? The help is appreciated. Thank you in advance.
-Vinny
--
---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/45xA52nsLBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.