Ios 通过触摸按钮显示广告横幅 解释

Ios 通过触摸按钮显示广告横幅 解释,ios,swift,sprite-kit,delegates,ads,Ios,Swift,Sprite Kit,Delegates,Ads,这个标题几乎是自作自受。我试图让一个按钮激活一个广告横幅,但是Xcode在下面返回了这个消息;因此,由于UIViewController,我看到应该在GameViewController上调用此横幅,而不是GameScene。我到处寻找它,但没有找到如何从游戏场景中调用此横幅或通过按钮激活此横幅的其他方法 无法将“GameSecene”类型的值转换为预期的参数类型“UIViewController!” 代码 下面的代码非常简单。它在试图调用广告横幅时有一个按钮 import SpriteKit

这个标题几乎是自作自受。我试图让一个按钮激活一个广告横幅,但是Xcode在下面返回了这个消息;因此,由于
UIViewController
,我看到应该在
GameViewController
上调用此横幅,而不是
GameScene
。我到处寻找它,但没有找到如何从
游戏场景中调用此横幅或通过按钮激活此横幅的其他方法

无法将“GameSecene”类型的值转换为预期的参数类型“UIViewController!”


代码 下面的代码非常简单。它在试图调用广告横幅时有一个按钮

import SpriteKit
import Ads

class GameScene: SKScene {

    var button = SKSpriteNode()

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        button = SKSpriteNode(imageNamed: "button")
        button.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
        button.setScale(0.4)
        addChild(button)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        for touch in touches {

            let location = touch.locationInNode(self)
            let node = nodeAtPoint(location)

            if node == button{
                Ads.showAd(AdsShowStyle.BannerBottom, rootViewController: self) //issue line
            }
        }
    }
}
导入SpriteKit
进口广告
类游戏场景:SKScene{
var button=SKSpriteNode()
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
按钮=SKSpriteNode(图像名称:“按钮”)
button.position=CGPoint(x:self.frame.width/2,y:self.frame.height/2)
按钮设置刻度(0.4)
addChild(按钮)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
接触{
let location=touch.locationInNode(自)
让节点=节点点(位置)
如果节点==按钮{
Ads.showAd(AdsShowStyle.bannerbotom,rootViewController:self)//发行行
}
}
}
}

企图 经过一些研究,我发现通过授权是可能的。根据投票率最高的问题答案,我得出了下面的代码,但我有很多问题,我正在努力解决,但没有成功


游戏场景。swift

GameViewController.swift


提前感谢,

Luiz.

由于您使用的是SpriteKit,但我们不知道您使用的是什么广告网络,因此我建议在GameViewController中使用NSNotificationCenter命令,并在GameSecene.swift中使用postNotificationName命令,而不是ViewControllerDelegate。这将允许在任何其他.swift文件中使用您的广告代码

注意:这可能有效,也可能无效,具体取决于广告网络

GameViewController.swift(来自我的项目)

GameSecene.swift(已编辑)

导入SpriteKit
进口广告
类游戏场景:SKScene{
var button=SKSpriteNode()
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
按钮=SKSpriteNode(图像名称:“按钮”)
button.position=CGPoint(x:self.frame.width/2,y:self.frame.height/2)
按钮设置刻度(0.4)
addChild(按钮)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
接触{
let location=touch.locationInNode(自)
if(按钮容器点(位置)){
NSNotificationCenter.defaultCenter().postNotificationName(“showAdMobKey”,对象:nil)
}
}
}
} 

你使用什么广告网络?
import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController, GADAdDelegate {


var adMobBanner : GADBannerView!


override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"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.scaleMode = .AspectFill

        skView.presentScene(scene)
    }



    adMobBanner = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
    adMobBanner.adUnitID = "your ad unit id"
    adMobBanner.rootViewController = self
    adMobBanner.frame = CGRectMake(0, view.bounds.height - adMobBanner.frame.size.height, adMobBanner.frame.size.width, adMobBanner.frame.size.height)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.showAdMob), name: "showAdMobKey", object: nil)




}


func showAdMob() {

    let request : GADRequest = GADRequest()
    adMobBanner.loadRequest(request)
    self.view.addSubview(adMobBanner)

    print("adMob")

}
import SpriteKit
import Ads

class GameScene: SKScene {

var button = SKSpriteNode()

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    button = SKSpriteNode(imageNamed: "button")
    button.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    button.setScale(0.4)
    addChild(button)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in touches {

        let location = touch.locationInNode(self)

        if (button.containsPoint(location)) {

            NSNotificationCenter.defaultCenter().postNotificationName("showAdMobKey", object: nil)

        }

     }
  }
}