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()函數,但我真的不確定。預先感謝您的幫助。
當我這樣做,我得到一個錯誤說「類型」SKScene「的值沒有成員」adDelegate「。謝謝 –
@PranavSharan啊,還有一個問題 - 請參閱我編輯的答案。 –
@PranavSharan你的代碼顯示從來沒有調用createGameOver - 是否從別的地方調用? –