ios - 如何在 GameOverScene 中从 GameViewController 调用方法

标签 ios swift iad

我正在 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()
}
}

控制台只显示“加载广告”,但不显示“广告已加载”。

最佳答案

终于明白了。

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

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

    controller.loadAd()

关于ios - 如何在 GameOverScene 中从 GameViewController 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31618946/

相关文章:

ios - UIFont "short"样式

ios - iAd ADBannerView 检测卸载

ios - XIB Control 单击以编程方式引用 socket iAD

ios - 如何将资源保存到 CloudKit sharedCloudDatabase?

ios - 我的导航栏是否违反了 IOS 人机界面指南

iphone - 如何在 IOS 5 上使用 Open CV 查找模式的出现?

ios - Xcode Storyboard 上的并排约束

swift - collectionview 上的非粘性标题部分单元格(补充 View )

ios - 如何阻止 iAds 在 xcode 中扭曲 SKScene?

ios - 如何在 swift 4 中将文本字段条目限制为小数点后两位?