2015-09-10 49 views
1

我一直在使用時,玩家死亡,加載IAD設備間質斯威夫特和雪碧套件做了一個遊戲。但是,廣告有時會延遲,因此在遊戲重新啓動時會在遊戲過程中彈出。有什麼辦法可以解決這個問題嗎?我把從我GameScene使用iAd的間隙:iAd的間質呈現延遲

NSNotificationCenter.defaultCenter().postNotificationName("showInterstitialAd", object: nil) 

GameViewController.swift

import UIKit 
import SpriteKit 
import iAd 

class GameViewController: UIViewController, ADBannerViewDelegate, ADInterstitialAdDelegate { 

    var interstitialAd:ADInterstitialAd! 
    var interstitialAdView: UIView = UIView() 
    let transition = SKTransition.fadeWithDuration(1) 

    var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     closeButton.frame = CGRectMake(10, 10, 20, 20) 
     closeButton.layer.cornerRadius = 10 
     closeButton.setTitle("x", forState: .Normal) 
     closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal) 
     closeButton.backgroundColor = UIColor.whiteColor() 
     closeButton.layer.borderColor = UIColor.blackColor().CGColor 
     closeButton.layer.borderWidth = 1 
     closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown) 

     //loadInterstitialAd() 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadInterstitialAd", name: "showInterstitialAd", object: nil) 

     let scene = GameScene(size:CGSize(width: 2208, height: 1242)) 
     // Configure the view. 
     let skView = self.view as! SKView 
     skView.showsFPS = false 
     skView.showsNodeCount = false 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

     /* Set the scale mode to scale to fit the window */ 
     scene.scaleMode = .AspectFit 
     scene.anchorPoint = CGPoint(x: 0, y: 0) 

     skView.presentScene(scene, transition: transition) 
    } 

    func close(sender: UIButton) { 
     closeButton.removeFromSuperview() 
     interstitialAdView.removeFromSuperview() 
     interstitialAd.delegate = nil 
    } 

    func loadInterstitialAd() { 
     interstitialAd = ADInterstitialAd() 
     interstitialAd.delegate = self 
    } 

    func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) { 
    } 

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) { 
     interstitialAd.delegate = self 
     interstitialAdView = UIView() 
     interstitialAdView.frame = self.view.bounds 
     view.addSubview(interstitialAdView) 
     view.addSubview(closeButton) 
     interstitialAd.presentInView(interstitialAdView) 
     UIViewController.prepareInterstitialAds() 
    } 

    func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) { 
     closeButton.removeFromSuperview() 
     interstitialAdView.removeFromSuperview() 
    } 

    func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool { 
     return true 
    } 

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) { 
     interstitialAdView.removeFromSuperview() 
     closeButton.removeFromSuperview() 
     interstitialAd.delegate = nil 
    } 

    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) { 
     closeButton.removeFromSuperview() 
     interstitialAdView.removeFromSuperview() 
     interstitialAd.delegate = nil 
    } 
+0

是的,幾乎肯定有辦法解決這個問題。如果你願意分享一些細節,也許我們甚至可以告訴你*如何* ... – stakx

+0

請添加您可能具有的任何相關代碼。 – Tro

回答

1

你已經在你的viewDidLoad註釋掉//loadInterstitialAd()。取消註釋。您需要儘早加載插頁式廣告,以便在決定展示廣告時將其完全加載並準備好顯示。

此外,你只要出示您的間隙,因爲它裝載在你func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!)。您應該創建一個功能或操作,以在您希望的時候呈現插頁式廣告,例如,當用戶執行特定操作或遊戲結束時。

檢查我的回答here。插頁式廣告會顯示一次我創建的UIButton,即@IBAction func presentAdButton