Swift Spritekit - AdMob Banner on Game Over Scene Only

235 views
Skip to first unread message

Brett

unread,
Oct 16, 2018, 2:33:15 AM10/16/18
to google-adm...@googlegroups.com
I programmatically created a game and added a banner to my GameViewController.swift.
It works great but it displays on every scene and I only wanted to display the banner in my MainMenu and GameOver scenes.
Would I be able to do that?


Code samples below:


GameViewController.swift
    var bannerView: GADBannerView!

 

     

   
override func viewDidLoad() {

       
super.viewDidLoad()

         

       
if let view = self.view as! SKView? {

            let scene
= MainMenuScene(size: CGSize(width: 1536, height: 2048))

             

           
// Present the scene

            view
.presentScene(scene)


 

            view
.ignoresSiblingOrder = true

            view
.showsFPS = false

            view
.showsNodeCount = false

       
}

         

       
// In this case, we instantiate the banner with desired ad size.

        bannerView
= GADBannerView(adSize: kGADAdSizeBanner)

         

        addBannerViewToView
(bannerView)

         

        bannerView
.adUnitID = "ca-app-pub-3940256099942544/2934735716"

        bannerView
.rootViewController = self

        bannerView
.load(GADRequest())

   
}

     

    func addBannerViewToView
(_ bannerView: GADBannerView) {

        bannerView
.translatesAutoresizingMaskIntoConstraints = false

        view
.addSubview(bannerView)

        view
.addConstraints(

           
[NSLayoutConstraint(item: bannerView,

                                attribute
: .bottom,

                                relatedBy
: .equal,

                                toItem
: view.safeAreaLayoutGuide,

                                attribute
: .bottom,

                                multiplier
: 1,

                                constant
: 0),

             
NSLayoutConstraint(item: bannerView,

                                attribute
: .centerX,

                                relatedBy
: .equal,

                                toItem
: view,

                                attribute
: .centerX,

                                multiplier
: 1,

                                constant
: 0)

           
])

   
}





MainMenuScene.swift 
//when clicking start button to move to GameScene
//want to display banner here
                let sceneToMoveTo = GameScene(size: self.size)

                let myTransition
= SKTransition.fade(withDuration: 0.5)

               
self.view?.presentScene(sceneToMoveTo, transition: myTransition)





GameScene.swift
//then when the game is over this code will run to go to the GameOverScene
//do NOT want to display banner here
        let sceneToMoveTo = GameOverScene(size: self.size)

        sceneToMoveTo
.scaleMode = self.scaleMode

        let myTransition
= SKTransition.fade(withDuration: 0.5)

       
self.view!.presentScene(sceneToMoveTo, transition: myTransition)





GameOverScene.swift
//want to display banner here

mobileadssdk-a...@google.com

unread,
Oct 16, 2018, 3:24:22 PM10/16/18
to Brett, Google Mobile Ads SDK Developers
Hi Brett,

Thanks for reaching out to us. You simply would have to hide the BannerAdView when a new Scene is being presented and only show for the scenes you require. I would suggest that you have a manager class to handle this and determine when to show and when not to.

Regards,
Deepika Uragayala
Mobile Ads SDK Team
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/dbb00ec6-2a20-4bb4-9c4d-6d0c337305e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brett

unread,
Oct 23, 2018, 8:06:10 PM10/23/18
to Google Mobile Ads SDK Developers
Thank you for your help. 

This is new to me and I was unable to find examples of a manager class. Would you please direct me towards a source where I can read on the subject and/or see examples of this functionality.

Would all of this be done within the GameViewController.swift? Or would I need to add code to the game scenes as well?

mobileadssdk-a...@google.com

unread,
Oct 24, 2018, 3:29:07 PM10/24/18
to Brett, Google Mobile Ads SDK Developers
Hi Brett,

This is a pure implementation issue and you can handle it via the Game Scene by delegating to the rootVC, where the BannerAdView is residing, whether to show or hide it from the screen. We do not have a sample app for this. That said, we will not be able to provide any official support for third party frameworks. If you have any other concerns regarding the SpriteKit implementation, I'd suggest that you post your concern on StackOverflow. 


Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages