2017-04-13 58 views
0

我试图在玩家死后在我的应用上显示插页式弹出式广告。我在ViewController中有一个函数,但我不知道如何在GameScene中使用它。我在视图控制器下面的代码:如何在Sprite Kit中显示弹出插页式广告?

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") 
    } 
} 

,我有我的GameScene下面的代码:

import SpriteKit 
import GameplayKit 
import AVFoundation 

class GameScene: SKScene, SKPhysicsContactDelegate { 

    var adDelegate: AdMobInterstitialDelegate? 

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

我不知道究竟是怎么了,我有一种感觉,我我在协议中错误地放入了createInterstitialAd()函数,但我真的不确定。预先感谢您的帮助。

回答

0

你正在创建的场景是SKScene,而不是你的子类GameScene。

变化:

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

要:

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

也因此它仍然是零,并调用createInterstitialAd绝不会因为发生条件解缠你不设置委托参考adDelegate(即?)。

后:

scene.scaleMode = .aspectFill 

地址:

scene.adDelegate = self 

(另外,你不需要 '自我'。在:

self.adDelegate?.createInterstitialAd() 

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

+0

当我这样做,我得到一个错误说“类型”SKScene“的值没有成员”adDelegate“。谢谢 –

+0

@PranavSharan啊,还有一个问题 - 请参阅我编辑的答案。 –

+0

@PranavSharan你的代码显示从来没有调用createGameOver - 是否从别的地方调用? –