Pause SpriteKit Scene when iAd is clicked in Swift

混江龙づ霸主 提交于 2019-12-10 19:07:22

问题


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.



来源:https://stackoverflow.com/questions/24794114/pause-spritekit-scene-when-iad-is-clicked-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!