2017-09-04 3 views
1

J'ai ajouté des publicités interstitielles dans mon jeu. En ce moment, j'ai l'annonce connectée à un bouton au centre de l'écran. Ce que je voudrais faire, c'est que l'annonce soit automatiquement affichée à chaque fois que le joueur meurt. Est-ce que quelqu'un sait comment faire ça?Affichage de mon nouvel interstitiel (admob) lorsque le joueur meurt

Ceci est mon code pour les annonces interstitielles:

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") 
} 
} 

Ceci est ma fonction 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() 
     } 
    } 

} 

Répondre

1

EDIT: J'ai cherché un peu plus sur SpriteKit et il semble que le moyen facile pour faire ce que vous voulez faire est avec des notifications.

UIViewController:

class GameViewController: UIViewController, GADBannerViewDelegate, 
GADInterstitialDelegate { 
    // rest of the controller code 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     NotificationCenter.default.addObserver(self, selector: #selector(openInterstitial), name: "playerDied", object: nil) 
    } 

    func openInterstitial() { 
     self.fullScreenAds = CreateAndLoadInterstitial() 
    } 

    // rest of the controller code 
} 

SKScene:

class GameScene: SKScene { 

    func playedDied() { 
     // rest of the method code 

     NotificationCenter.default.post(name: "playerDied", 
            object: nil) 
    } 
} 
+0

J'ai effectué mon texte ci-dessus. Bien que j'ai maintenant 3 erreurs. J'ai probablement mis certaines choses au mauvais endroit. – Flinigan

+0

L'extension doit être hors de portée de votre classe GameViewController pas à l'intérieur. Edited ma réponse. – Jeremy

+0

Oh, ça a corrigé l'erreur. Un autre problème, où dois-je mettre le code "var gameScene = GameScene (...)" et "gameScene.interstitialDelegate = self"? – Flinigan