2016-02-04 26 views
0

我有這些facebook插頁式廣告,當我在他們的GameScene中調用他們時,他們不顯示。當我在GameViewController中調用它們時,它們完美地工作,但不在我的GameScene中。有人能告訴我我的代碼在做什麼錯嗎?爲什麼我的插頁式廣告不能在我的GameScene中工作,而是在Swift的GameViewController中工作?

//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() 
     } 
     } 
     } 

回答

0

在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() 
+0

好了,所以我嘗試了你的代碼,但廣告仍然彈不出來,我在控制檯中得到這個警告:警告:嘗試在上呈現,它已經呈現 coding22

+0

沒關係它的作品!謝謝!! – coding22

+0

賞金如果您的問題得到解決。至於'UIActivityController'警告,你是否顯示'UIActivityController'?如果是這樣,請確保在嘗試呈現廣告之前將其解除。 – uchiha

0

你試試:

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

var scene: GameScene! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let skView = view as! SKView 
    skView.multipleTouchEnabled = false 
    scene = GameScene(size: skView.bounds.size) 
    scene.scaleMode = .AspectFill 
    scene.view?.userInteractionEnabled = true 
    skView.presentScene(scene) 
    scene.gvcDelegate = self 
} 

    //GameScene.swift 
class GameScene: SKScene { 
    gvcDelegate: GameViewController? 
    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" { 
      gvcDelegate.loadFBInterstitialAd() 
     } 
     } 
    } 

古德勒克!

+0

好吧現在試試吧。 – coding22

+0

whats gvcdelegate? – coding22

+0

gvcdelegate被替換GameViewController – chaunv

相關問題