0

I am making a game with SpriteKit in Swift and I am having trouble when displaying the ad. The ad shows up when the player dies, as it is supposed to, but instead of staying in the current scene (GameScene) when I close it, it switches out to the MainMenu scene for some reason. Actually, if you watch closely, you can see that the "game over" text appears and as the ad slides up to display, it switches to the MainMenu scene. I saw the post here and tried what the answers said (moving everything from ViewWillLayoutSubviews to ViewDidLoad, etc) and it didn't work. Any ideas? Thanks.

Here is all of the code involving the ad in GameViewController:

class GameViewController: UIViewController, UIAlertViewDelegate, GADInterstitialDelegate {

var musicPlayer = AVAudioPlayer()

var musicUrl = Bundle.main.url(forResource: "Loops2.mp3", withExtension: nil)

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)

    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)

}

func playerDied() {
    if self.interstitial.isReady {
        self.interstitial.present(fromRootViewController: self)
    }
}

func loadAd() -> GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/1033173712")
    interstitial.delegate = self
    interstitial.load(GADRequest())
    return interstitial
}

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    NotificationCenter.default.post(name: NSNotification.Name("AdDismissed"), object: nil)
    interstitial = loadAd()
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    if let url = musicUrl {
        try! musicPlayer = AVAudioPlayer(contentsOf: url)

        musicPlayer.numberOfLoops = -1
        musicPlayer.prepareToPlay()
        musicPlayer.play()

    }

}

I use the NotificationCenter to signal GameViewController when to display the ad. I don't think that the code from GameScene is needed, but if you think it would be helpful, just ask. Thanks!

Jake
  • 1
  • 3
  • Please clean up your code, you are doing your code in both viewDidLoad and viewWilllayoutSubviews – Knight0fDragon Dec 15 '17 at 17:36
  • Just fixed it. Well would you look at that. That did the trick! Thank you! – Jake Dec 17 '17 at 02:02
  • `viewWillLayoutSubviews` will be called, well... just before every subview layout (so basically lots of times, on every event that adds views, redraws, screen rotation, etc). Don't use that unless you need to fix frames (and that should be done in `viewDidLayoutSubviews`). Maybe you wanted to use `viewDidAppear:`? – Alejandro Iván Dec 17 '17 at 02:17

0 Answers0