我已將橫幅視圖集成到我的應用程序中的一個場景中,但我未能將Interstitial廣告集成到另一個場景中。 這裏是我的代碼: 進口SpriteKit 進口的GameKit 進口GoogleMobileAds在連接到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")
}
}
當它試圖與加載它失敗「致命錯誤:意外發現零而展開的可選值」。問題出現在下面,就好像代碼是這樣顯示的,我在應用程序啓動時加載GameOver場景可以正常工作。我怎樣才能解決這個問題?
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)
}