Search code examples
iosswiftsprite-kitinterstitial

Change Scenes after Interstitial (SpriteKit)


Here is the code that runs when the restart button is tapped. The game resets the score, plays and interstitial ad, and then goes back to the game scene.

Right now it does not wait for the interstitial to be exited before changing the game scene.

  if restartButton.contains(pointOfTouch) {

            score = 0

            displayAd()



            let sceneToMoveTo = GameScene(size: self.size)
            sceneToMoveTo.scaleMode = self.scaleMode
            let myTransition = SKTransition.fade(withDuration: 0.5)
            self.view!.presentScene(sceneToMoveTo, transition: myTransition)

        }

Solution

  • So I was able to do was include a wait function, that way the ad is shown on the screen before the scene even transitions.

     func delay(_ delay:Double, closure:@escaping ()->()) {
             let when = DispatchTime.now() + delay
             DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
     }
    

    And then placed scene changer within the delay function like so

     delay(2.0) {
        self.changeScene()
     }
    

    The 2.0 is the time in seconds so this is saying that the code will wait 2 seconds and then run the change scene function. When the ad is exited the scene is now changed.