2015-09-10 40 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