通过触摸按钮显示广告横幅
Show ad banner by touching a button
说明
这个标题本身就是个问题。我试图让一个按钮激活一个广告横幅,但是 Xcode returns 我收到了下面的这条消息;因此,由于 UIViewController
,我看到应该在 GameViewController
上调用此横幅,而不是 GameScene
。我到处寻找它,但没有找到如何从 GameScene
调用此横幅或通过按钮激活此横幅的其他方式。
Cannot convert value of type 'GameScene' to expected argument type '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
}
}
}
}
尝试
经过一些研究,我发现使用委托可能是可行的。根据 问题的最高投票答案,我想到了下面的这段代码,但我有很多问题,我正在努力解决,但没有成功。
GameScene.swift
GameViewController.swift
提前致谢,
路易斯
由于您使用的是 SpriteKit 但我们不知道您使用的是哪个广告网络,我建议在 GameViewController 中使用 NSNotificationCenter 命令并在 GameScene.swift 中使用 postNotificationName 命令而不是 ViewControllerDelegate。这将允许您的广告代码用于任何其他 .swift 文件。
注意:这可能有效也可能无效,具体取决于广告网络
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)
}
}
}
}
说明
这个标题本身就是个问题。我试图让一个按钮激活一个广告横幅,但是 Xcode returns 我收到了下面的这条消息;因此,由于 UIViewController
,我看到应该在 GameViewController
上调用此横幅,而不是 GameScene
。我到处寻找它,但没有找到如何从 GameScene
调用此横幅或通过按钮激活此横幅的其他方式。
Cannot convert value of type 'GameScene' to expected argument type '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
}
}
}
}
尝试
经过一些研究,我发现使用委托可能是可行的。根据
GameScene.swift
GameViewController.swift
提前致谢,
路易斯
由于您使用的是 SpriteKit 但我们不知道您使用的是哪个广告网络,我建议在 GameViewController 中使用 NSNotificationCenter 命令并在 GameScene.swift 中使用 postNotificationName 命令而不是 ViewControllerDelegate。这将允许您的广告代码用于任何其他 .swift 文件。
注意:这可能有效也可能无效,具体取决于广告网络
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)
}
}
}
}