2017-04-19 57 views
0

我有一個精靈套件遊戲,我想在遊戲結束時顯示插頁式廣告。那麼,我可以在遊戲結束後使用NotificationCenter展示廣告,但問題在於當我關閉廣告時,它會返回到WelcomeScene。我想在關閉廣告後返回到GameOverScene,但是如何? GameViewController.swift當GameOver顯示插頁式廣告後,場景返回到WelcomeScene

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) 


} 

}

GameScene.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() 



} 

HUD.swift

import SpriteKit 

    class HUD : SKNode { 
    var scoreLabel = SKLabelNode(text: "0") 
    let restartBut = SKSpriteNode() 
    let menuBut = SKSpriteNode() 
    let highScoreLabel = SKLabelNode() 

    func createNode(screenSize : CGSize){ 

    scoreLabel.fontName = "AppleSDGothicNeo-SemiBold" 
    scoreLabel.position = CGPoint(x: (screenSize.width/2) - (screenSize.width/4 * 2) , y: (screenSize.height/2) - 50) 
    scoreLabel.fontColor = UIColor.black 
    scoreLabel.fontSize = 40 
    scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center 
    scoreLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center 
    scoreLabel.zPosition = 50 
    self.addChild(scoreLabel) 

    //Restart and Menu button 
    restartBut.texture = SKTexture(imageNamed: "restartButton") 
    restartBut.name = "restartButton" 
    restartBut.position = CGPoint(x: 0, y: 0) 
    restartBut.zPosition = 50 
    restartBut.size = CGSize(width: 400, height: 400) 

    menuBut.texture = SKTexture(imageNamed: "menuButton") 
    menuBut.name = "menuButton" 
    menuBut.position = CGPoint(x: 0, y: -300) 
    menuBut.zPosition = 50 
    menuBut.size = CGSize(width: 150, height: 150) 

    highScoreLabel.fontName = "AppleSDGothicNeo-SemiBold" 
    highScoreLabel.position = CGPoint(x: 0, y: 450) 
    highScoreLabel.fontColor = UIColor.black 
    highScoreLabel.fontSize = 45 
    highScoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center 
    highScoreLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center 
    highScoreLabel.zPosition = 50 
    } 

    func showRestartMenu(){ 

    restartBut.alpha = 0 
    menuBut.alpha = 0 
    highScoreLabel.alpha = 0 

    self.addChild(restartBut) 
    self.addChild(menuBut) 
    self.addChild(highScoreLabel) 

    restartBut.run(SKAction.fadeAlpha(to: 1, duration: 0.35)) 
    menuBut.run(SKAction.fadeAlpha(to: 1, duration: 0.35)) 
    highScoreLabel.run(SKAction.fadeAlpha(to: 1, duration: 0.35)) 





    } 

    func updateScore(score : Int){ 
    var highScore = UserDefaults().integer(forKey: "highScore") 
    let number = NSNumber(value: score) 
    let formatter = NumberFormatter() 

    if let scoreText = formatter.string(from: number){ 
     scoreLabel.text = scoreText 
    } 




    if Int(score) > highScore { 
     highScore = Int(score) 
     highScoreLabel.text = NSString.init(format: "Highscore : %i", highScore) as String 

     let highScoreDefs = UserDefaults.standard 
     highScoreDefs.set(highScore, forKey: "highScore") 
     highScoreDefs.synchronize() 

    } 

    highScoreLabel.text = "Highscore : \(highScore)" 

} 

回答

1

轉到您的GameViewController和移動從ViewWillLayoutSubviews所有代碼爲ViewDidLoad。只有在加載GameViewController時才調用ViewDidLoad。另一方面,ViewWillLayoutSubviews可以多次調用,就像您的廣告展示時一樣,這會比再次加載您的MenuScene。

作爲一個提示,你應該把你的通知鍵放入一個擴展名中,以避免錯別字,並將其作爲一個良好實踐的更具體的名稱。

所以添加此上述任何一類或一個新雨燕文件

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

我也注意到,你在你的GameViewController規模模式下使用.resizeFill。你不應該這樣做,你的遊戲在每個設備上看起來都不一樣,這將是一個噩夢來管理。您應該使用默認的scaleMode .aspectFill,這通常是最好的設置。

根據視圖的邊界(view.bounds.size),您還應該給場景一個固定的大小並且不要調整它的大小。

How to make SKScene have fixed width?

How do I size sprites in a universal sprite kit game

希望這有助於

+0

我已經更新了代碼。當玩家(遊戲角色)死亡,並且廣告出現並關閉廣告時,當前場景消失,而是顯示菜單場景(這是我的應用啓動後的第一個場景.... * sry,而不是歡迎使用*)的方式。我期待它向我展示在遊戲結束時將其添加到HUD並添加到HUD的restartButton。 –

+0

我還沒有看到代碼表明任何場景轉換正在發生。請顯示轉到GameOverScene的轉換代碼 – crashoverride777

+0

我也注意到您在GameViewController縮放模式下使用了.ResizeFill。你不應該這樣做,你的遊戲在每個設備上看起來都不一樣,這將是一個噩夢來管理。您應該使用默認的scaleMode .aspectFill,這通常是最好的設置。 – crashoverride777

相關問題