我有这些Facebook非页内广告,当我在GameScene中调用它们时,它们不会显示。当我在GameViewController中而不是在GameScene中调用它们时,它们可以完美工作。有人可以告诉我我的代码在做什么错吗?

//GameViewController.swift

class GameViewController: UIViewController, FBInterstitialAdDelegate {
let interstitialFBAD: FBInterstitialAd = FBInterstitialAd(placementID: "65543456456456_454345345454534")


override func viewDidLoad() {
    super.viewDidLoad()

}

//CALL THIS IN MY GAMESCENE.
func loadFBInterstitialAd() {
interstitialFBAD.delegate = self
interstitialFBAD.loadAd()
}

func interstitialAdDidLoad(interstitialAd: FBInterstitialAd!) {
    interstitialFBAD.showAdFromRootViewController(self)
}

//GameScene.swift

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)

        if node.name == "retry" {

            GameViewController().loadFBInterstitialAd()
        }
        }
        }

最佳答案

在GameViewController的viewDidLoad中注册一个通知,然后在GameScene中发布一个想要显示广告的通知。

因此您的viewDidLoad将有以下额外的行

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadFBInterstitialAd", name: "loadAd", object: nil)


然后在Gamescene中编写一个方法

func showAdNow() {
    NSNotificationCenter.defaultCenter().postNotificationName("loadAd", object: nil)
}


现在在Gamescene.swift中替换行

GameViewController().loadFBInterstitialAd()




showAdNow()

09-10 11:56