J'ai intégré une vue de bannière dans une scène de mon application, mais je n'arrive pas à intégrer une annonce interstitielle dans une autre scène. Voici mon code: importation SpriteKit importation GameKit GoogleMobileAds importationPrésentation d'une annonce interstitielle dans un jeu sur une scène liée à GameViewController?
class GameOverMenu: SKScene, GKGameCenterControllerDelegate, UIAlertViewDelegate {
var viewController: GameViewController!
var interstitial: GADInterstitial!
var myTimer = Timer()
override func didMove(to view: SKView) {
createAndLoadInterstitial()
startMyTimer()
}
func createAndLoadInterstitial() {
interstitial = GADInterstitial(adUnitID: "...")
let request = GADRequest()
request.testDevices = [ kGADSimulatorID, "..." ]
interstitial.load(request)
}
func startMyTimer() {
myTimer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(GameOverMenu.myFunction), userInfo: nil, repeats: false)
}
func myFunction(){
if interstitial.isReady {
interstitial.present(fromRootViewController: viewController)
} else {
print("Ad wasn't ready")
}
}
Il échoue quand il essaie de charger avec « erreur fatale: de façon inattendue trouvée nulle en déballant une valeur en option ». Le problème se situe ci-dessous comme si le code est affiché comme ça et je charge la scène GameOver lorsque l'application se lance, ça fonctionne très bien. Comment puis-je réparer cela?
if let view = self.view as! SKView? {
// Load the SKScene from 'MainMenu.sks'
if let scene = MainMenuScene(fileNamed: "MainMenu") {
scene.viewController = self
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
if let scene3 = GameOverMenu(fileNamed: "GameOver") {
scene3.viewController = self
// Set the scale mode to scale to fit the window
scene3.scaleMode = .aspectFill
view.presentScene(scene3)
}