2016-11-03 71 views
1

我試圖在我的精靈套件遊戲中創建激勵視頻廣告。我想在我的GameScene.swift類中添加獎勵視頻,但問題是這不是ViewController,而是SKSceneSKScene中的AdMob激勵視頻

這不工作,因爲我GameScene.swift不是ViewController

ad.present(fromRootViewController: self) 

我嘗試過很多事情,但毫無效果。有人能幫助我嗎? 謝謝!

(我要當玩家死亡,顯示這種回報的廣告。)

+0

你必須從GameViewController中展示它。使用通知讓SKScene告訴GameViewController啓動視頻廣告 – Nik

+0

@Nik我試圖在GameViewController中創建函數,並且不會調用GameScene中的函數,但這不起作用。當它不得不調用函數時,它給了我同樣的錯誤。 –

+0

使用通知在場景和視圖控制器之間進行通信。我會盡快答覆 – Nik

回答

1

在你GameViewController,設置觀察者在viewWillLayoutSubviews

override func viewWillLayoutSubviews() { 

    NotificationCenter.default.addObserver(self, selector: #selector(self.startVideoAd), name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil) 

} 

func startVideoAd() { 

// Do something - play video ad 

} 

在這種情況下,每當這個通知被調用時, GameViewController裏面的函數名爲startVideoAd將會運行。很明顯,你會想把名字改成你想要運行的函數的名字。

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil) 

希望這有助於:

然後,在你GameScene,發送通知,你會無論/只要你想運行裏面GameViewController功能運行此!