标题几乎要求自己。我试图按下按钮激活广告横幅,但Xcode会在下面给我回复此消息;所以,由于UIViewController
,我看到应该在GameViewController
上调用此横幅,而不是GameScene
。我到处寻找它,但没有找到如何从GameScene
或其他方式调用此横幅以通过按钮激活此横幅。
无法转换类型' GameScene'的值预期的参数类型' 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
}
}
}
}
经过一些研究,我发现使用委托可能有可能。在this问题的最高投票答案之后,我在下面的代码中提到了这个问题,但是我遇到了很多问题,但我没有成功,难以解决。
的 GameScene.swift
GameViewController.swift
提前谢谢,
路易斯。
答案 0 :(得分:1)
由于您使用的是SpriteKit,但我们不知道您使用的广告网络,我建议您在GameViewController中使用NSNotificationCenter命令,并在GameScene.swift中使用postNotificationName命令而不是ViewControllerDelegate。这样,您的广告代码就可以在任何其他.swift文件中使用。 Here's a better explanation of this from another post if needed
注意:这取决于广告网络可能有效,也可能无效
GameViewController.swift(来自我的项目)
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")
}
GameScene.swift(编辑你的)
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)
}
}
}
}