0

I have a spritekit game and I have no experience working with the storyboard or scenes. I've added the following code into my GameViewController file:

class adScene : SKScene, ADInterstitialAdDelegate { var interAd:ADInterstitialAd? var interAdView = UIView() var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

override func didMoveToView(view: SKView) {

    // Try to display ad
    let adShown = showAd()

    // If ad doesn't show
    if adShown == false {
        println("something broke")

    }

    closeButton.frame = CGRectMake(20, 20, 70, 44)
    closeButton.layer.cornerRadius = 10
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.whiteColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    closeButton.addTarget(self, action: "closeAd:", forControlEvents: UIControlEvents.TouchDown)
    closeButton.enabled = false
    closeButton.setTitle("skip", forState: UIControlState.Normal)
    closeButton.enabled = true
    closeButton.setNeedsLayout()
}

func prepareAd() {
    println(" --- AD: Try Load ---")
    // Attempt to load new ad:
    interAd = ADInterstitialAd()
    interAd?.delegate = self
}

func showAd() -> Bool {
    if interAd != nil && interAd!.loaded {
        interAdView = UIView()
        interAdView.frame = self.view!.bounds
        self.view?.addSubview(interAdView)

        interAd!.presentInView(interAdView)
        UIViewController.prepareInterstitialAds()

        interAdView.addSubview(closeButton)
    }

    // Return true if showing an ad, otherwise false:
    return interAd?.loaded ?? false
}

// When the user clicks the close button, route to the adFinished function:
func closeAd(sender: UIButton) {
    adFinished()
}

// A function of common functionality to run when the user returns to your app:
func adFinished() {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
    // (Do whatever is next in your app)
}

// The ad loaded successfully
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Load Success ---")
}

// The ad unloaded
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Unload --- ")
}

// This is called if the user clicks into the interstitial, and then finishes interacting with the ad
// We'll call our adFinished function since we're returning to our app:
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
    println(" --- ADD: Action Finished --- ")
    adFinished()
}

func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
    return true
}

// Error in the ad load, print out the error
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println(" --- AD: Error --- ")
    println(error.localizedDescription)
}

Then, in GameScene, I create var myInterAd = adScene(), then prepare an ad by myInterAd.prepareAd(), then try to present the scene by self.view?.presentScene(myInterAd, transition: .crossFadeWithDuration(0.6)).

But the screen just turns black when the scene should load. I don't know how to properly use the storyboard, I created a View Controller scene (linking it to ViewController() but I'm missing something. Thanks.

edit: does my code do the work of switching to the other scene(the ViewController Scene) and then back to the GameScene? Or do I need to do something in the storyboard?

1 Answers1

0

You dont really need to use a SKScene just to show an ad. You want to overlay the add on your current scene using your viewController.

If you check out my market answer in this question you should be able to integrate interstitial ads in no time. Its very simple.

Interstitial iAds in SpriteKit?

Community
  • 1
  • 1
crashoverride777
  • 10,581
  • 2
  • 32
  • 56