podomunro
podomunro

Reputation: 693

Scene stretches when loading iAd in SpriteKit (landscape mode)

When I try to load an iAd in SpriteKit, the scene resets and starts again, whilst also running the scene it had already loaded in the background at the same time. I created a BOOL to help determine whether the scene has already loaded, and if so, then there's no need to load it again. The iAd displays fine, but the scene itself stretches across the screen. Here's my code from my ViewController.m:

@implementation ViewController
{
    BOOL _sceneLoaded;
}


- (void)awakeFromNib
{
    [super awakeFromNib];

    [self setCanDisplayBannerAds:YES];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView *skView = nil;

    if (self.originalContentView) {
        skView = (SKView *)self.originalContentView;
    }else{
        skView = (SKView *)self.view;
    }

    [skView setShowsDrawCount:NO];
    [skView setShowsFPS:NO];
    [skView setShowsNodeCount:NO];

        if (_sceneLoaded == NO) {
    SKScene *scene = [MainMenuScene sceneWithSize:skView.bounds.size];
    [scene setScaleMode:SKSceneScaleModeFill];
    [skView presentScene:scene];
    _sceneLoaded = YES;
        }
}

I'm not quite sure where I'm going wrong. I've tried called the if (_sceneLoaded == NO) statement in different areas of the code, only to achieve the same result. Thanks in advance for any help.

Upvotes: 0

Views: 278

Answers (2)

iC7Zi
iC7Zi

Reputation: 1658

Below code is tested in iOS 7 and 8 and works just fine.

Add below lines in header file

#import <iAd/iAd.h>

    @interface GameViewController : UIViewController<ADBannerViewDelegate>{

        //iAd
        ADBannerView *adView;

    }

In Implementation file .m add below code

#import "GameViewController.h"
#import "GameScene.h"

@implementation GameViewController

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    SKView *skView;

    if (self.originalContentView) {

        skView = (SKView *)self.originalContentView;

    }

    if (!skView.scene) {

        //[skView setShowsDrawCount:YES];
        //[skView setShowsFPS:YES];
        //[skView setShowsNodeCount:YES];

        //Improve Performance
        skView.ignoresSiblingOrder = YES;

        GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
        [scene setScaleMode:SKSceneScaleModeFill];

        [skView presentScene:scene];

    }

}

- (void)awakeFromNib{

    [super awakeFromNib];

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectMake(0, 0, screenRect.size.width, adView.frame.size.height);
    adView.delegate=self;
    [self.view addSubview:adView];
}

//iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {

    [adView setAlpha:1.0];
    NSLog(@"Show Ad");

}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {

    [adView setAlpha:0];
    NSLog(@"Hide Ad");

}

//

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

-(BOOL)prefersStatusBarHidden{

    return YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end

Upvotes: 2

user1233894
user1233894

Reputation: 1776

if your using [self setCanDisplayBannerAds:YES] this will shrink your view to make room for the iAd banner. I would suggest adding the ADBannerViewDelegate to your view controller and animating the banner into the view... See this tutorial http://codewithchris.com/iad-tutorial/

Upvotes: 1

Related Questions