如何在 GameOverScene 中从 GameViewController 调用方法

How to call methods from GameViewController in GameOverScene

我正在 GameViewController 中设置一个插页式广告,我想调用在 GameOverScene 中加载广告的函数。但是,GameViewController 连接到 GameScene,而不是 GameOverScene。如何在 GameOverScene 中调用 loadAd() 函数?

GameViewController 代码:

import iAd

extension SKNode {
class func unarchiveFromFile(file : String) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

class GameViewController: UIViewController, ADInterstitialAdDelegate {

var interAd = ADInterstitialAd()
var interAdView: UIView = UIView()

var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

override func viewDidLoad() {
    super.viewDidLoad()


    closeButton.frame = CGRectMake(10, 10, 20, 20)
    closeButton.layer.cornerRadius = 10
    closeButton.setTitle("x", forState: .Normal)
    closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.blackColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)



    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseTimers:"), name:UIApplicationWillResignActiveNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil)

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.size = skView.bounds.size
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

func close(sender: UIButton) {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}

func loadAd() {
    println("load ad")
    interAd = ADInterstitialAd()
    interAd.delegate = self

}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println("ad did load")

    interAdView = UIView()

    interAdView.frame = self.view.bounds
    view.addSubview(interAdView)

    interAd.presentInView(interAdView)
    UIViewController.prepareInterstitialAds()

    interAdView.addSubview(closeButton)
}


func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {


}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println("failed to receive")
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()

}

}

GameOverScene 中,我尝试调用 loadAd() 函数,但它没有显示广告。

import Foundation

import SpriteKit

import UIKit

import iAd

class GameOverScene : SKScene {

override func didMoveToView(view : SKView) {
    //Background Color
    scene?.backgroundColor = UIColor.blackColor()

    GameViewController().loadAd()
}
}

控制台只显示 "load ad" 但不显示 "ad did load"。

终于想通了。

我在 GameOverScene 的 didmovetoview 中添加了这段代码

    let controller = self.view?.window?.rootViewController as! GameViewController

    controller.loadAd()