Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Sprite套件中显示弹出式间隙广告?_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 如何在Sprite套件中显示弹出式间隙广告?

Ios 如何在Sprite套件中显示弹出式间隙广告?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我试图在玩家死后在我的应用程序上显示一个插入式弹出广告。我在ViewController中有一个函数,但我不知道如何在游戏场景中使用它。我在视图控制器中有以下代码: import UIKit import SpriteKit import GameplayKit import GoogleMobileAds protocol AdMobInterstitialDelegate{ func createInterstitialAd() } class GameViewControll

我试图在玩家死后在我的应用程序上显示一个插入式弹出广告。我在ViewController中有一个函数,但我不知道如何在游戏场景中使用它。我在视图控制器中有以下代码:

import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds

protocol AdMobInterstitialDelegate{
    func createInterstitialAd()
}


class GameViewController: UIViewController, GADBannerViewDelegate, AdMobInterstitialDelegate, GADInterstitialDelegate {

let scene = GameScene()

var interstitial: GADInterstitial!

@IBOutlet weak var adbanner: GADBannerView!

override func viewDidLoad() {

    interstitial = GADInterstitial(adUnitID: "ca-app-pub-1279952988973855/9087084328")
    let request = GADRequest()
    // Request test ads on devices you specify. Your test device ID is printed to the console when
    // an ad request is made.
    request.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9b" ]
    interstitial.load(request)

    //self.createInterstitialAd()


    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }

    }
}

func createInterstitialAd(){
    if interstitial.isReady {
        interstitial.present(fromRootViewController: self)
    } else {
        print("Ad wasn't ready")
    }
}
我的游戏场景中有以下代码:

import SpriteKit
import GameplayKit
import AVFoundation

class GameScene: SKScene, SKPhysicsContactDelegate {

    var adDelegate: AdMobInterstitialDelegate?

    func createGameOver(){ 
    self.adDelegate?.createInterstitialAd()
}

我不知道到底出了什么问题,我感觉我在协议中错误地使用了createInterstitutionAlad()函数,但我真的不确定。提前感谢您的帮助。

您正在创建的场景是一个SKScene,而不是您的子类游戏场景

更改:

if let scene = SKScene(fileNamed: "GameScene") {
致:

此外,您没有设置委托引用adDelegate,因此它保持为零,并且由于条件展开(即?)的原因,对CreateInterstituralad的调用永远不会发生

之后:

scene.scaleMode = .aspectFill
加:

(此外,您不需要“自我”。在:

self.adDelegate?.createInterstitialAd()

但这不会引起问题,只是没有必要。)

当我这样做时,我会得到一个错误,即“SKScene”类型的值没有成员“adDelegate”。Thanks@PranavSharan啊,还有一个问题-请看我编辑过的答案。@PranavSharan还有您显示的代码从未调用createGameOver-是从其他地方调用的吗?
scene.adDelegate = self
self.adDelegate?.createInterstitialAd()