繁体   English   中英

如何在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()
}
}

控制台仅显示“加载广告”,而不显示“广告已加载”。

终于想通了。

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

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

    controller.loadAd()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM