我有这个游戏项目,并且有一个投币系统。我还为用户提供了一个观看视频广告的选项,以使其总数增加50个硬币。由于商店页面是SKScene,我通过视图控制器运行广告,并在完成展示后,视图控制器在场景内运行一个功能,该功能应该更新硬币数量和显示它们的标签。
这是我的视图控制器的代码:
class GameViewController: UIViewController , GADRewardBasedVideoAdDelegate {
var rewardBasedVideo: GADRewardBasedVideoAd!
var rewardvideoisinprogress = false
var interstitial: GADInterstitial!
var adcounter = 0
override func viewDidLoad() {
super.viewDidLoad()
adcounter = UserDefaults.standard.integer(forKey: "AdCounter")
print(adcounter)
createandloadvideoad()
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3343126174384559/7308554354")
let request = GADRequest()
interstitial.load(request)
ThemeShop().updatecoins()
NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.presentvideoad), name: NSNotification.Name("video"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.inter), name: NSNotification.Name("inter_"), object: nil)
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "MainMenu") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
}
}
@objc func inter(){
adcounter = adcounter + 1
print(adcounter)
if adcounter == 2{
adcounter = 0
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
interstitial = createad()
} else {
print("Ad wasn't ready")
}
}
UserDefaults.standard.set(adcounter, forKey: "AdCounter")
}
func createad() -> GADInterstitial {
let inter = GADInterstitial(adUnitID: "ca-app-pub-3343126174384559/7308554354`")
inter.load(GADRequest())
return inter
}
func createandloadvideoad(){
rewardBasedVideo = GADRewardBasedVideoAd.sharedInstance()
rewardBasedVideo?.delegate = self
if !rewardvideoisinprogress && rewardBasedVideo?.isReady == false{
//ca-app-pub-3343126174384559/3425197396
rewardBasedVideo?.load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
rewardvideoisinprogress = true
}
}
@objc func presentvideoad(){
if rewardBasedVideo?.isReady == true{
rewardBasedVideo?.present(fromRootViewController: self)
}else{
print("Was NOt Reardyadjfsjfsalfkj")
}
createandloadvideoad()
}
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
didFailToLoadWithError error: Error) {
print("Reward based video ad failed to load: \(error.localizedDescription)")
rewardvideoisinprogress = false
}
func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Reward based video ad is received.")
}
func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Opened reward based video ad.")
}
func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
print("Reward based video ad started playing.")
}
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
rewardvideoisinprogress = false
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1500) , execute: {
print("Brikdsajfaslkd")
ThemeShop().updatecoins()
})
}
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
didRewardUserWith reward: GADAdReward) {
var coins = UserDefaults.standard.integer(forKey: "Coins")
coins = coins + 50
UserDefaults.standard.set(coins, forKey: "Coins")
}
}
这是SKScene内部的功能:
func updatecoins(){
print("Updating")
coins = UserDefaults.standard.integer(forKey: "Coins")
print("Coins: \(coins)")
self.coinlabel.text = String(self.coins)
print(self.coinlabel.text)
}
我知道硬币会被添加到幕后的天平中,因为如果我退出现场并重新进入,硬币就会像应该的那样出现。
答案 0 :(得分:0)
您要在ThemeShop的类变量上调用函数,您需要在该类的实例变量上调用func。
您没有显示ViewController代码,因此您可能必须对此进行相应调整
在viewController顶部具有一个场景变量
private var themeShop: ThemeShop!
然后在加载场景时在viewDidLoad中将其放在变量内
//this is if you are loading your scene from an sks file.
//your code may be different if it is just loaded from code with no sks file
if let scene = SKScene(fileNamed: "ThemeShop") {
self.themeShop = scene
然后在需要访问场景时在viewController中
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
rewardvideoisinprogress = false
sleep(1)
themeShop.updatecoins()
}
答案 1 :(得分:0)
我认为您的标签未更新,因为您不在Index time [s/1000] PPG Elapsed Time
0 2019-05-27 15:17:22+00:00 29441.0 -00:00:00.04
1 2019-05-27 15:17:22.013333252+00:00 29544.0 -00:00:00.027
2 2019-05-27 15:17:22.026666748+00:00 29645.0 -00:00:00.014
3 2019-05-27 15:17:22.040000+00:00 29743.0 0
4 2019-05-27 15:17:22.053333252+00:00 29837.0 00:00:00.013
队列中。我也对您的代码进行了一些更改。您不应该使用main
更好地使用sleep()
:
asyncAfter