如果你曾经玩过Flappy Bird并完成了一个游戏,则会在应用程序顶部显示一个广告,直到你开始一个新游戏。我想这么做,但是我很难将所需的代码传递到gameOver函数(如果可能的话)。
我需要为这个添加一个全新的场景吗?还是我可以在游戏场景类中添加?
我知道要显示广告,请转到ViewController.swift并执行以下操作:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: CGSize(width: 2048, height: 1536))
let skView = view.self as! SKView
skView.ignoresSiblingOrder = false
scene.scaleMode = .AspectFill
skView.presentScene(scene)
self.canDisplayBannerAds = true
}
}
唯一的问题是它会一直显示广告,即使是在游戏运行的时候。一旦进入gamescope.swift文件,广告仍在运行。所以,我试图将viewDidLoad()添加到gamescree.swift文件(整个游戏屏幕)中。我的游戏没有标题场景或游戏结束场景。这些场景发生在gamescope类中;它所做的只是显示一个图像)。
我把这个加入到游戏场景中了。斯威夫特,但它什么也没做。
class adBannerView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
}
}
当gameOver()函数聚焦时,如何弹出广告?或者游戏结束后呢?我试过看其他问题,但它们与我的不同。我不能只在gameOver函数中添加self.canDisplayAds=true。
注意,代码是必需的是Swift 2.0,我使用的是xcode7prerelease2。
最佳答案
从self.canDisplayBannerAds = true
中移除viewDidLoad
并将其添加到gameover函数中。然后在重新启动游戏的函数中添加self.canDisplayBannerAds = false
。这将删除iAd横幅。
func gameOver() {
// Display ad when game ends
self.canDisplayBannerAds = true
}
func replayGame() {
// Game will restart so remove ad
self.canDisplayBannerAds = false
}
请注意,当您显示iAd横幅时,它可能需要一秒钟才能加载。