如何从GameOverScene中的GameViewController调用方法

时间:2015-07-24 20:07:45

标签: ios swift iad

我正在GameViewController设置插页式广告,我想在GameOverScene中调用加载广告的功能。但是,GameViewControllerGameScene相关联,而不是GameOverScene。如何调用loadAd()中的GameOverScene功能?

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()
}
}

控制台仅显示"加载广告"但不是"广告确实加载了#34;。

1 个答案:

答案 0 :(得分:1)

终于明白了。

我将此代码添加到GameOverScene中的didmovetoview

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

    controller.loadAd()