Jake
Jake

Reputation: 1

Scene changes when displaying AdMob ad in Swift

I recently implemented AdMob into my game and I am having trouble displaying the ad in my GameScene. When the app loads up, I load the MainMenu scene from the GameViewController like so:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let skView = self.view as! SKView
    skView.ignoresSiblingOrder = true
    skView.showsFPS = true
    skView.showsNodeCount = true

    let mainMenu = MainMenu()
    mainMenu.scaleMode = .aspectFill
    mainMenu.size = view.bounds.size
    skView.presentScene(mainMenu)
}

After the player presses start, I change the scene to GameScene and the player plays the game until they die. I added an observer to GameScene to tell the GameViewController to display the ad once they die. I created the observer in viewDidLoad like this:

var interstitial: GADInterstitial!

override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = loadAd()
    NotificationCenter.default.addObserver(self, selector: #selector(self.playerDied), name: NSNotification.Name("ShowAd"), object: nil)
    NotificationCenter.default.post(name: NSNotification.Name("ShowingAd"), object: nil)

}

So, when the player dies, I post "ShowAd" to the notification center and the ad goes through the .isReady check and displays. However, after the ad is dismissed, it reloads the mainMenu scene insted of just resuming gameScene. Does anyone know of a way to display the ad but have the current scene still be GameScene after the ad is displayed? Thanks.

Upvotes: 0

Views: 58

Answers (1)

KissTheCoder
KissTheCoder

Reputation: 679

I'm guessing that the viewController's func viewWillLayoutSubviews() is being called when the ad is dismissed. Try putting all of the code that is in func viewWillLayoutSubviews() into override func viewWillAppear(_ animated: Bool)

Upvotes: 0

Related Questions