1
我在我的遊戲中添加了一些插頁式廣告。截至目前,我已將廣告連接到屏幕中央的按鈕。我想要做的是在播放器死亡時自動顯示廣告。有誰知道如何做到這一點?當玩家死亡時顯示我新添加的插頁式廣告(admob)
這是我的插頁式廣告代碼:
import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds
//I added this
protocol GameInterstitialDelegate {
func openInterstitial()
}
class GameViewController: UIViewController, GADBannerViewDelegate,
GADInterstitialDelegate {
var fullScreenAds :GADInterstitial!
//I added this
extension GameViewController: GameInterstitialDelegate {
func openInterstitial() {
self.fullScreenAds = CreateAndLoadInterstitial()
}
}
func CreateAndLoadInterstitial() -> GADInterstitial? {
fullScreenAds = GADInterstitial(adUnitID: "ca-app-pub-
6532333990655924/8291640688")
guard let fullScreenAds = fullScreenAds else {
return nil
}
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
fullScreenAds.load(request)
fullScreenAds.delegate = self
return fullScreenAds
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print("ads loaded")
ad.present(fromRootViewController: self)
}
func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
print("ad did not load")
}
}
這是我playerDied功能:
class GameScene: SKScene {
//I added this
weak var interstitialDelegate: GameInterstitialDelegate?
//I added this
var gameScene = GameScene(fileNamed: "GameScene")
gameScene.interstitialDelegate = self
func playedDied() {
self.removeAction(forKey: "SpawnCar2")
self.removeAction(forKey: "SpawnCar3")
self.removeAction(forKey: "SpawnCoin")
for child in children {
if child.name == "Car2" {
child.removeAction(forKey: "MoveCar2")
} else if child.name == "Car3" {
child.removeAction(forKey: "MoveCar3")
} else if child.name == "Coin" {
child.removeAction(forKey: "MoveCoin")
}
}
isAlive = false
let highscore = GameManager.instance.getHighscore()
if highscore < score {
GameManager.instance.setHighscore(highscore: score)
}
let retry = SKSpriteNode(imageNamed: "Retry")
let quit = SKSpriteNode(imageNamed: "Quit")
retry.name = "Retry"
retry.anchorPoint = CGPoint(x: 0.5, y: 0.5)
retry.position = CGPoint(x: -150, y: -150)
retry.zPosition = 7
retry.setScale(0)
quit.name = "Quit"
quit.anchorPoint = CGPoint(x: 0.5, y: 0.5)
quit.position = CGPoint(x: 150, y: -150)
quit.zPosition = 7
quit.setScale(0)
let scaleUp = SKAction.scale(to: 0.8, duration: TimeInterval(0.5))
retry.run(scaleUp)
quit.run(scaleUp)
self.addChild(retry)
self.addChild(quit)
//I added this
interstitialDelegate?.openInterstitial()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event:
UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if atPoint(location).name == "Play" {
gameplay!.scaleMode = .aspectFill
self.view?.presentScene(gameplay!, transition:
SKTransition.fade(withDuration: TimeInterval(1)))
}
if atPoint(location).name == "Highscore" {
scoreLabel.removeFromParent()
createLabel()
}
}
}
我編輯我上面的文字。雖然我現在有3個錯誤。我可能把一些東西放在了錯誤的地方。 – Flinigan
擴展應該在你的GameViewController類的範圍之外,而不是在裏面。編輯我的答案。 – Jeremy
哦,那修正了錯誤。還有一個問題,我應該在哪裏放置代碼「var gameScene = GameScene(...)」和「gameScene.interstitialDelegate = self」? – Flinigan