本文介绍了SKScene中的AdMob奖励视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在我的 sprite kit 游戏中制作一款奖励视频广告。我想在我的 GameScene.swift 类中添加奖励视频,但是问题是这不是 ViewController 而是 SKScene

I'm trying to create a rewarded video ad in my sprite kit game. I want to add the rewarded video in my GameScene.swift class but the problem is that this isn't a ViewController but a SKScene.

这不起作用,因为我的 GameScene.swift 不是 ViewController

This doesn't work because my GameScene.swift isn't a ViewController:

ad.present(fromRootViewController: self)

我尝试了很多事情,但没有任何效果。有谁可以帮助我吗?
谢谢!

I've tried many things but nothing worked. Can someone help me please?Thanks!

(我想在玩家死亡时显示此奖励广告。)

(I want to display this rewarded ad when the player dies.)

推荐答案

在您的 GameViewController 中,在 viewWillLayoutSubviews 中设置观察器:

In your GameViewController, setup the observer in viewWillLayoutSubviews:

override func viewWillLayoutSubviews() {

    NotificationCenter.default.addObserver(self, selector: #selector(self.startVideoAd), name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil)

}

func startVideoAd() {

// Do something - play video ad

}

在这种情况下,无论何时调用此通知后,将运行 GameViewController 中名为 startVideoAd 的函数。显然,您希望将名称更改为要运行的函数的名称。

In this case, whenever this notification is called, the function inside GameViewController named startVideoAd will be run. Obviously you would want to change the name to the name of your function that you want to be run.

然后,在您的 GameScene 中,要发送通知,无论何时何地,您都可以运行此通知 GameViewController 内的函数:

Then, in your GameScene, to send the notification, you would run this wherever/whenever you want to run the function inside GameViewController:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil)

希望这会有所帮助!

Hope this helps!

这篇关于SKScene中的AdMob奖励视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:36