gpt4 book ai didi

ios - 如何在 Sprite Kit 中显示弹出式插页式广告?

转载 作者:行者123 更新时间:2023-11-30 12:32:09 24 4
gpt4 key购买 nike

我试图在玩家死亡后在我的应用程序上显示插页式弹出广告。我在 ViewController 中有一个函数,但我不知道如何在 GameScene 中使用它。我在 View Controller 中有以下代码:

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() 函数错误地放入了协议(protocol)中,但我真的不确定。预先感谢您的帮助。

最佳答案

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

更改:

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

致:

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

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

之后:

scene.scaleMode = .aspectFill

添加:

scene.adDelegate = self

(此外,您不需要“ self ”:

self.adDelegate?.createInterstitialAd()

但这不会导致问题,只是没有必要。)

关于ios - 如何在 Sprite Kit 中显示弹出式插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43383250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com