How do I enable ads in a Swift SceneKit game when it's game over?

| | August 11, 2015

So if you have ever played Flappy Bird and get a game over, an ad gets displayed on the top of the app until your start a new game. I want to do that, but I’m having a hard time passing the required code into the gameOver function (if possible somehow).

Do I have to add a whole new scene for this or can I just do it in the class of the GameScene?

I know to display ads, you go to the ViewController.swift and do this:

class GameViewController: UIViewController {

  override func viewDidLoad() {
   super.viewDidLoad()
   let scene = GameScene(size: CGSize(width: 2048, height: 1536))
   let skView = view.self as! SKView
   skView.ignoresSiblingOrder = false
   scene.scaleMode = .AspectFill
   skView.presentScene(scene)

   self.canDisplayBannerAds = true
  }
}

The only problem with this is that it will display ads the entire time, even when the game is running. As soon as it moves in to the GameScene.swift file, the ads are still running. So, I tried to add the viewDidLoad() into the GameScene.swift file (which is the whole game screen. My game has no title scene or gameover scene. Those scenes take place in the GameScene class; all it does is show an image).

I added this into the GameScene.swift but it doesn’t do anything.

class adBannerView: UIViewController {

 override func viewDidLoad() {

   super.viewDidLoad()

   self.canDisplayBannerAds = true

  }
}

How do I make ads pop up when the gameOver() function ocurs? Or when it’s game over? I’ve tried looking at other questions but they were different than mine. I can’t just add self.canDisplayAds = true inside the gameOver function.

Note that code is required is Swift 2.0 and I’m using xCode 7 Prerelease 2.

One Response to “How do I enable ads in a Swift SceneKit game when it's game over?”

  1. Remove self.canDisplayBannerAds = true from your viewDidLoad and add it to your gameover function. Then in your function that restarts the game add self.canDisplayBannerAds = false. This will remove the iAd banner.

    func gameOver() {
        // Display ad when game ends
        self.canDisplayBannerAds = true
    }
    
    func replayGame() {
        // Game will restart so remove ad
        self.canDisplayBannerAds = false
    }
    

    Note that it may take a second for the iAd banner to load when you display it.

Leave a Reply