我已经做了一个游戏使用迅捷和精灵工具包加载一个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
}
最佳答案
你已经在你的//loadInterstitialAd()
中注释掉了viewDidLoad
。取消对此的注释。您需要尽早加载间隙,以便在您决定显示间隙时,它已完全加载并准备好显示。
而且,一旦填隙物加载到func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!)
中,您就可以显示它。您应该创建一个函数或操作,以便在需要时显示间隙,例如当用户执行某个操作或游戏结束时。
检查我的答案here。一旦点击我创建的UIButton
,间隙就会显示出来。