2017-01-31 8 views
0

アプリケーション内のシーンにバナービューを統合しましたが、別のシーンにインタースティシャル広告を組み込むことができません。 輸入SpriteKitベース インポートGameKit 輸入GoogleMobileAdsGameViewControllerにリンクしているゲームオーバーレイにインタースティシャル広告を表示しますか?

class GameOverMenu: SKScene, GKGameCenterControllerDelegate, UIAlertViewDelegate { 

var viewController: GameViewController! 
var interstitial: GADInterstitial! 

var myTimer = Timer() 


override func didMove(to view: SKView) { 

createAndLoadInterstitial() 

startMyTimer() 




} 


func createAndLoadInterstitial() { 
interstitial = GADInterstitial(adUnitID: "...") 
let request = GADRequest() 
request.testDevices = [ kGADSimulatorID, "..." ] 
interstitial.load(request) 
} 


func startMyTimer() { 
myTimer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(GameOverMenu.myFunction), userInfo: nil, repeats: false) 

} 

func myFunction(){ 

if interstitial.isReady { 
    interstitial.present(fromRootViewController: viewController) 
} else { 
    print("Ad wasn't ready") 
} 

} 

それがでロードしようとするとき、それが失敗している「致命的なエラー:予期せずにオプションの値をアンラップしながら、nilを見つけ、」 は、ここに私のコードです。この問題は、コードが次のように表示され、アプリケーションの起動時にGameOverシーンが読み込まれるように、問題が発生します。これをどうすれば解決できますか?

if let view = self.view as! SKView? { 




    // Load the SKScene from 'MainMenu.sks' 
    if let scene = MainMenuScene(fileNamed: "MainMenu") { 

     scene.viewController = self 

     // Set the scale mode to scale to fit the window 
     scene.scaleMode = .aspectFill 

     // Present the scene 
     view.presentScene(scene) 


    } 

    if let scene3 = GameOverMenu(fileNamed: "GameOver") { 

     scene3.viewController = self 

     // Set the scale mode to scale to fit the window 
     scene3.scaleMode = .aspectFill 

     view.presentScene(scene3) 





    } 

答えて

0

問題は、2つのシーンの間を遷移するときに、アプリケーションを起動したとき、それはのみ動作し、なぜあなたはGameViewController例えば

scene3.viewController = self 

ザッツへの参照を失うということです。

あなたも使用しています!それらのプロパティについて

var viewController: GameViewController! 
var interstitial: GADInterstitial! 

もしそうでないとクラッシュします。だからあなたはいつも使うべきですか?あなたが何かがあることを100%確信していないとき。

var viewController: GameViewController? 
var interstitial: GADInterstitial? 

"myFunction"などのコードでは、 "?"を使用します。プロパティがnilのときはクラッシュしないように "if let"を指定します。

問題の一般的な解決方法は、すべてのAdMobコードをGameViewControllerに直接移動する必要があることです。 NotificationCenterやデリゲートのようなものを使って、シーンからViewControllerにメッセージを転送して広告を表示することができます。あなたのSKScenesでViewControllerを参照するのは実際にはベストプラクティスではありません。

だから、あなたがオブザーバーを追加することができますのviewDidLoadでGameViewControllerに比べ

extension Notification.Name { 
     static let showAd = Notification.Name(rawValue: "NotificationShowAd") 
} 

    class GameViewController: UIViewController {... 

キーの通知のためのこの拡張機能を作成するクラスの実装の外GameViewControllerであなたのViewControllerにし、よりすべての広告コードを移動

override func viewDidLoad() { 
    super.viewDidLoad() 

     createAndLoadInterstitial() 

     NotificationCenter.default.addObserver(self, selector: #selector(myFunction), name: .showAd, object: nil) 

     .... 
} 

今すぐSKScenesの広告を表示する必要がある場合は、

NotificationCenter.default.post(name: .showAd, object: nil) 
さらに簡単にGitHubの

https://github.com/crashoverride777/SwiftyAds

の私のヘルパーを見ているあなたの人生を作るために

が、これは

を役に立てば幸い
関連する問題