2016-07-11 3 views
0

私はGameViewControllerで宣言されたインタースティシャル広告を持っています。私はボタンで広告を呼び出すチュートリアルに従いましたが、GameSceneから電話する必要がありますが、クラスを呼び出す方法はわかりません。すべてのクラス間でグローバル変数を使用できますか? 基本的に私はGameSceneでfunc adButton()を呼びたいと思っています。swift:ゲームシーンでViewControllerで作成した広告を呼び出す方法

また、報酬広告を実装する方法はありますか、それとも誰かに良いチュートリアルがありますか?

ありがとうございました。

func createAndLoadInterstitial() -> GADInterstitial { 

    let interstitial = GADInterstitial(adUnitID: " unit id ") 
    let request1 = GADRequest() 
    interstitial.delegate = self 
    request1.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9b" ] 
    interstitial.loadRequest(request1) 
    return interstitial 




} 


func adButton() { 

    if (self.interstitial.isReady) 
    { 
     self.interstitial.presentFromRootViewController(self) 
     self.interstitial = self.createAndLoadInterstitial() 
    } 
} 

答えて

2

使用notifictionCenter

がスイフトでのviewDidLoad()とadButton第二()

にあるスウィフト3

//In the gameviewcontroller 
    NotificationCenter.default.addObserver(self, selector: #selector(YourFunctionNameHere), name: "NSNotificationCreate" as NSNotification.Name, object: nil) 
//In the gameScene use this to call the ad 
    NotificationCenter.default.post(name: "NSNotificationCreate" as NSNotification.Name, object: nil) 

を第一の機能を追加する機能を呼び出します2

//In the gameviewcontroller 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "YourFunctionNameHere", name: "NSNotificationCreate", object: nil) 

//In the gameScene use this to call the ad 
    NSNotificationCenter.defaultCenter().postNotificationName("NSNotificationCreate", object: nil) 
+0

私はこれに永遠に詰め込まれていたので、まったく完璧に働いた。この通知センターは、クラス間で/プログラム全体を通じて情報を送信する方法ですか?私はそれを必要としませんが、それはどのような機能のために使用することができますか?再度、感謝します。 – yellowlan

+0

これを使用して、別のViewControllerまたはGameScene @yellowlan上の関数を呼び出すことができます – danielpp95

+0

完璧、ありがとう。 – yellowlan

関連する問題