Pause Spritekit Scene When Iad Is Clicked in Swift

Running a function (pause menu) when iAd is clicked SpriteKit Swift

Try this function for banner iAd:

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("Clicked")

// If you want to pause game scene:
let skView: SKView = self.view as! SKView
skView.scene.paused = true

return true
}

and also:

func bannerViewActionDidFinish(banner: ADBannerView!) {
println("Closed")

// If you want to continue game scene:
let skView: SKView = self.view as! SKView
skView.scene.paused = false
}

Pause SpriteKit Scene When Clicking on iAds

If you have an SKView that's presenting a scene, you can call pause from within its ViewController like this:

Objective-C:

SKView *skView = (SKView *)self.view;
skView.scene.paused = YES;

Swift

let skView: SKView = self.view as! SKView
skView.scene.paused = true

iAd in Sprite Kit Pauses the scene

There is usually a problem when using self.canDisplayBannerAds with spritekit. I have tried endlessly to get it to work, but to no avail. Instead, I use this code:

adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[adView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:adView];

First initialize a ADBannerView *adView variable in your Viewcontroller's interface file, then you can use the above code in your ViewDidLoad method.

It works perfectly for me, and doesn't have any issues with pausing or unresponsive screen after clicking the ad.

How to effectively pause a game in spritekit?

Pausing a node will pause (not stop) the node's actions, and i suppose pause will also be applied to all children of the paused node in the same way. All other unpaused nodes will continue to run their actions to completion.

You should have a "game layer" node with all game nodes in them that you want to pause, and then just pause that "game layer" node. Then have a "pause game menu" node where you add all the pause menu stuff you need which will continue to function normally even if the game layer is paused.

Be very sure you do not use performSelector or NSTimer or GCD or any other scheduling methods besides actions or the scene's update: method because those will not adhere to a node's paused state.

Pausing the view effectively freezes everything in it, pausing the scene according to some will not pause the update: calls - but I have not verified this myself. If this were the case you can always check inside the update method whether a given node that you send a message to is paused and if so, decide not to send certain messages to that node.

Interstitial iAds in SpriteKit?

You can make use of my helper I posted on github. Its was made specifically for spritekit and you can call ads from anywhere without delegates etc or changing view controllers.

https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper

I think having a look at that helper should give you a good idea of where to start. Here is a cut down example of how it could look just for iAds Inter ads.

This is in swift so I am not sure if it is still helpful for you.

import iAd

class Ads: NSObject {

// MARK: - Static Properties

/// Shared instance
static let sharedInstance = Ads()

// MARK: - Properties

/// Presenting view controller
var presentingViewController: UIViewController!

/// iAd inter ad
private var iAdInterAd: ADInterstitialAd?

/// iAd inter ad view
private var iAdInterAdView = UIView()

/// iAd inter ad close button
private var iAdInterAdCloseButton = UIButton(type: UIButtonType.System)

// MARK: - Init
private override init() {
super.init()
print("Ads helper init")

iAdInterAd = iAdLoadInterAd()
}

// MARK: - User Methods

/// Show inter ad
func showInterAd() {
iAdShowInterAd()
}

/// Show inter ad randomly (33% chance)
func showInterAdRandomly() {
let randomInterAd = Int(arc4random() % 3)
print("randomInterAd = \(randomInterAd)")
if randomInterAd == 1 {
iAdShowInterAd()
}
}

/// Remove all ads
func removeAllAds() {
print("Removed all ads")

if iAdInterAd != nil {
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}

// MARK: - Internal Methods

/// iAd load inter ad
private func iAdLoadInterAd() -> ADInterstitialAd {
print("iAd inter ad loading...")
let iAdInterAd = ADInterstitialAd()
iAdInterAd.delegate = self

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
iAdInterAdCloseButton.frame = CGRectMake(18, 18, 27, 27)
} else {
iAdInterAdCloseButton.frame = CGRectMake(13, 13, 22, 22)
}

iAdInterAdCloseButton.layer.cornerRadius = 11
iAdInterAdCloseButton.setTitle("X", forState: .Normal)
iAdInterAdCloseButton.setTitleColor(UIColor.grayColor(), forState: .Normal)
iAdInterAdCloseButton.backgroundColor = UIColor.whiteColor()
iAdInterAdCloseButton.layer.borderColor = UIColor.grayColor().CGColor
iAdInterAdCloseButton.layer.borderWidth = 2
iAdInterAdCloseButton.addTarget(self, action: "iAdPressedInterAdCloseButton:", forControlEvents: UIControlEvents.TouchDown)

return iAdInterAd
}

/// iAd show inter ad
private func iAdShowInterAd() {
guard iAdInterAd != nil else {
print("iAd inter is nil, reloading")
iAdInterAd = iAdLoadInterAd()
return
}

if iAdInterAd!.loaded {
print("iAd inter showing")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)

//pauseTasks() // not really needed for inter as you tend to show them when not playing.
} else {
print("iAd inter not ready, reloading again...")
iAdInterAd = iAdLoadInterAd()

}
}

/// iAd inter ad pressed close button
func iAdPressedInterAdCloseButton(sender: UIButton) { // dont make private as its called with a selector
print("iAd inter closed")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
iAdInterAd = iAdLoadInterAd()

//resumeTasks() // not really needed for inter as you tend to not show them during gameplay
}

/// Pause tasks in the app/game
private func pauseTasks() {
// Pause app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}

/// Resume tasks in the app/game
private func resumeTasks() {
// Resume app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
}

// MARK: - Delegates iAd Inter
extension Ads: ADInterstitialAdDelegate {

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
print("iAd inter did load")
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
print("iAd inter did unload")
}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
print("iAd inter error \(error)")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}

Now you simply call

Ads.sharedInstance.presentingViewController = self 

in your GameViewController before doing anything else. This will init the helper and preload the first inter ad.

Than you simply call these anywhere you would like inter ads to show

GameData.sharedInstance.showInterAd()

or

GameData.sharedInstance.showInterAdRandomly() // 33% chance for ad

Why won't my game pause when an iAd is tapped on in Swift?

I think your problem is here:

let skView = self.view as SKView
skView.paused = true

and I think you should check if skView is your game view. self.view might be the ad view at that point.

To fix it, find your view, instead of using self.

//at top of class
var window:UIWindow?

// inside my method/function
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
println("Found it!!!")
viewController.paused = true
}
}
}


Related Topics



Leave a reply



Submit