2016-07-11 24 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