1

我已成功实施 AdMob 激励视频广告。我已经在 GameViewController 类中实现了它,并使用 NotificationCenter 方法将它调用到 gameScene 中,它正在工作。当我在视频完成后关闭奖励视频时,我的 GameScene 变成黑屏。这是我的 GameViewController 代码:

import UIKit
import SpriteKit
import GoogleMobileAds
class GameViewController: UIViewController, GADBannerViewDelegate, GADRewardBasedVideoAdDelegate, UIAlertViewDelegate{

var rewardBasedVideo:GADRewardBasedVideoAd?

var adRequestInProgress = false

override func viewDidLoad() {

    super.viewDidLoad()
    // create our scene:
    let menuScene = MenuScene 
    let skView = self.view as! SKView
    skView.showsPhysics = true
    skView.ignoresSiblingOrder = true
    let size = CGSize(width:590, height:390)
    menuScene.size = CGSize(width: size.width, height: size.height)
    skView.presentScene(menuScene)
    skView.translatesAutoresizingMaskIntoConstraints = false

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

override var supportedInterfaceOrientations : UIInterfaceOrientationMask{
    return UIInterfaceOrientationMask.landscape;
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden : Bool {
    return true
}



func videoReward(){
    rewardBasedVideo = GADRewardBasedVideoAd.sharedInstance()
    rewardBasedVideo?.delegate = self
    rewardBasedVideo?.load(GADRequest(),
                        withAdUnitID: "ca-app-pub-3940256099942544/1712485313")

    if rewardBasedVideo?.isReady == true {
        rewardBasedVideo?.present(fromRootViewController: self)
    } else {
    UIAlertView(title: "Reward based video not ready",
                message: "The reward based video didn't finish loading or   failed to load",
                delegate: self.view,
                cancelButtonTitle: "ok").show()
    }
}
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
                        didFailToLoadWithError error: Error) {
    adRequestInProgress = false
    print("Reward based video ad failed to load: \(error.localizedDescription)")
}

func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
    adRequestInProgress = false
    rewardBasedVideo?.present(fromRootViewController: self)

    print("Reward based video ad is received.")
}

}

我调用奖励视频的 GameScene 代码如下所示:

override func touchesBegan(_ touches: Set<UITouch>, with
    event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let nodeTouched = atPoint(location)
        if let gameSprite = nodeTouched as? GameSprite{
            gameSprite.onTap()

        }

      if nodeTouched.name == "play" {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue:"showVideoRewardAd"), object:nil)
        }
}

现在告诉我,我的错误在哪里?

4

0 回答 0