在GameOver上显示非页内广告后,场景返回WelcomeScene

时间:2017-04-19 08:19:11

标签: swift sprite-kit admob

我有一个sprite kit游戏,我想在游戏结束时显示插页式广告。好吧,我可以在游戏过后使用expecting显示广告,但问题是,当我关闭广告时,它会返回到WelcomeScene。关闭广告后我想回到GameOverScene但是怎么样? GameViewController.swift

NotificationCenter

}

GameScene.swift

import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds

class GameViewController: UIViewController , GADInterstitialDelegate {


var interstitialAds : GADInterstitial!


override func viewDidLoad() {
    super.viewDidLoad()



    createAndLoadAd()
    NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.showAds), name: NSNotification.Name("notification"), object: nil)


}


func createAndLoadAd(){
    let request = GADRequest()
    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    request.testDevices = [kGADSimulatorID]
    interstitial.delegate = self
    interstitial.load(request)
    interstitialAds = interstitial
}

func showAds(){

    if interstitialAds.isReady {
        interstitialAds.present(fromRootViewController: self)
    }
}

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    createAndLoadAd()
}


override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

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

override var prefersStatusBarHidden: Bool {
    return true
}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    let menu = Menu()
    let skView = self.view as! SKView

    skView.ignoresSiblingOrder = true

    menu.size = view.bounds.size
    menu.scaleMode = SKSceneScaleMode.resizeFill
    skView.presentScene(menu)


}

HUD.swift

    func gameOver(){

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

    //reset everything
    self.run(SKAction.playSoundFileNamed("Sound/die.wav", waitForCompletion: false))
    hud.updateScore(score: Int(score))


    player.die()
    hud.showScore()
    hud.showRestartMenu()



}   

1 个答案:

答案 0 :(得分:1)

转到GameViewController,将所有代码从ViewWillLayoutSubviews移至ViewDidLoadViewDidLoad只在GameViewController加载时调用一次。另一方面,ViewWillLayoutSubviews可以多次调用,例如展示广告时,会再次加载您的MenuScene。

作为提示,您应该将通知密钥放入扩展名以避免拼写错误,并且通过良好做法更具体地说明姓名。

所以在任何类或新的swift文件中添加这个

extension Notification.Name {
    static let showAd = Notification.Name("ShowAdNotification")
}

您可以将观察者改为此

NotificationCenter.default.addObserver(self, selector: #selector(showAds), name: .showAd, object: nil)

NotificationCenter.default.post(name: .showAd, object: nil)

如果你想要一个更清洁,更可重复使用的解决方案,你也可以在GitHub上查看我的帮助。

https://github.com/crashoverride777/SwiftyAds

我还注意到您在.resizeFill缩放模式下使用了GameViewController。你不应该这样做,你的游戏在每台设备上都会有所不同,这将是一场噩梦。您应该使用默认的scaleMode .aspectFill,这通常是最佳设置。

您还应该为场景指定固定大小,而不是根据视图的边界(view.bounds.size)调整大小。

How to make SKScene have fixed width?

How do I size sprites in a universal sprite kit game

希望这有帮助