2017-04-13 55 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 - 是否從別的地方調用? –

相關問題