Reputation: 19
I have a game with Ad Mob Ads. I am using SpriteKit. The ads work perfectly but I only want them to appear in certain scene. For example have them disappear in the game scene. My ad banner is set up as follows. In my main.storyboard I have a 320x50 UIView that is connected to a GADBannerView in my GameViewController. Everything works perfectly fine, but in every scene there is a banner ad. How can I make the banner only appear in the main menu scene and game over scene. These are SKScenes. Please help! Thanks!
p.s. I am using Swift
Upvotes: 0
Views: 443
Reputation: 467
Here's one way to implement that:
In your ViewController.swift, define
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAd:", name: "gameStateOff", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideAd:", name: "gameStateOn", object: nil)
Use these to communicate between your scene and your view controller.
Next, implement the showAd and hideAd functions, that will be called when your scene enters or leaves 'gaming state':
func showAd(notif: NSNotification) {
// this func places the ad to the bottom of the screen
var frame = ad.frame
frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
frame.origin.y = view.bounds.height - frame.size.height
ad.frame = frame
}
func hideAd(notif: NSNotification) {
// this func places the ad outside the screen
var frame = ad.frame
frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
frame.origin.y = -frame.size.height
ad.frame = frame
}
where ad is your GADBannerView(adSize: kGADAdSizeBanner).
Next add this to where you start the game
NSNotificationCenter.defaultCenter().postNotificationName("gameStateOn", object: nil)
This call makes the view controller execute hideAd function.
When you want to show the ad again, call
NSNotificationCenter.defaultCenter().postNotificationName("gameStateOff", object: nil)
Upvotes: 2