Pause SpriteKit Scene when iAd is clicked in Swift

2019-07-03 16:00发布

问题:

I'm implementing banner ads in my game. With the help of @erdekhayser the banner was displayed successfully. But when I tap on the banner, the game won't pause. The code looks like this:

import UIKit
import SpriteKit
import iAd
import Foundation
class GameViewController: UIViewController, ADBannerViewDelegate{

var gameScene = GameScene()
 var adBannerView = ADBannerView(frame: CGRect.zeroRect)


func loadAds() {

    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
    adBannerView.delegate = self
    view.addSubview(adBannerView)
}
//iAd bannerView
func bannerViewWillLoadAd(banner: ADBannerView!) {

}

func bannerViewDidLoadAd(banner: ADBannerView!){
    loadAds()
   println("1")
    adBannerView.hidden = false //now show banner as ad is loaded
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
  println("2")
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
   //Tap to view the ad

    gameScene.paused = true


    println("3")

    return true
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
       adBannerView.hidden = true
    println("44444")
}
// iAd stopped here ///////////
}

Anyone helps me figure it out?

回答1:

OK, I solved it myself, maybe not the best way. I added

  if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as SKView
        skView.paused = true

    }

in the bannerViewActionShouldBegin method. Add it worked.