Claudio13 Claudio13 - 4 months ago 6x
Swift Question

Pass function in ViewController to GameOverScene.swift

I tried to pass my function in ViewController to the GameOverScene. I saw a lot of answers but nothing worked for me. So i hope someone can help me.
The function i made is for interstitial AdMob which i get from the tutorial of greeky lemons on youtube. If the player died i want to show the Ad.

So in the GameViewController.swift i use this function:

func ShowAd(sender: AnyObject) {

if (interstital.isReady) {

interstital = CreateAd()



And now i don't know how to use this function in my GameOverScene.swift.
Has anyone an idea how to fix that? Please let me know if you need more informations. Thank you very much! :)


I literally answered a similar question today, the easiest way is NSNotification Center

Essentially in your view controller with your showAd function add an observer in viewDidLoad.

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showAd(_:)), name: "ShowInterAdKey", object: nil)

Selector is the function to call and name is the key to reference this observer.

Than in your game scene when you want to show the ad you post the notification with your key.

 NSNotificationCenter.defaultCenter().postNotificationName("ShowInterAdKey", object: nil)

Check out this answer for more detail because your code looks very similar to what I answered. You should create the ad before presenting it, not the other way round. To make it even better you should actually preload it way earlier rather when trying to show it.

Swift Admob Interstitial Error