我添加了这些StartApp插页式广告,当我在GameScene中调用此函数时,总是收到错误消息。如果我在GameViewController中调用它,则它可以完美运行,没有错误,但在GameScene中不起作用。我该如何解决。谢谢!

//这是给出错误的函数。


  self.viewController.startAppAd!.loadAdWithDelegate(viewController.self)


//GameViewController.swift

class GameViewController: UIViewController, STADelegateProtocol  {

    var startAppAd: STAStartAppAd?

    override func viewDidLoad() {
        super.viewDidLoad()

        startAppAd = STAStartAppAd()

        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
    }


    // StartApp Ad loaded successfully
    func didLoadAd(ad: STAAbstractAd) {
        println("StartApp Ad had been loaded successfully")
        startAppAd!.showAd()
    }
}

//GameScene.swift
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch: UITouch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)

    if node.name == "levelone" {

    self.viewController.startAppAd!.loadAdWithDelegate(viewController.self)
}

最佳答案

GameViewControllerGameScene期间创建viewDidLoad,但是您绝不会将其分配给GameScene进行引用

您说您在var viewController = GameViewController()中有GameScene,但是那与最初创建场景的实例不同。

您可以将此添加到您的viewDidLoad

scene.viewController = self以确保具有非nil startAppAd对象的实例是您正在引用的实例。

关于ios - 我不断收到此错误“在展开一个可选值时意外发现nil”,为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30784124/

10-12 05:42