Add ADBannerView to a SKScene

别等时光非礼了梦想. 提交于 2020-01-13 07:04:24

问题


I'm trying to add an iAd banner to my game over scene, but I don't know how to add UIView to an SKScene.

The app view controller is:

- (void)viewWillLayoutSubviews
    {
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [GameOverScene sceneWithSize:skView.bounds.size andScore:0];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

and the GameOverScene:

#import "GameOverScene.h"
#import <iAd/iAd.h>
@implementation GameOverScene

+(id)sceneWithSize:(CGSize)size andScore:(NSInteger)score{
     return [[self alloc] initWithSize:size andScore:score];
}
-(id)initWithSize:(CGSize)size andScore:(NSInteger)score{
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        //some stuff here...

        ADBannerView* banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
        [self.scene.view addSubview:banner];

        //[self.view addSubview:banner];

    }
    return self;
}
@end

Any idea or suggestion? Thanks!


回答1:


Use NSNotificationCenter. To make it simple for you:

1) Add a AdBannerView in the storyboard inside the SKView. (Or you can add in using code in your ViewController.m)

2) In your ViewController.h define ADBannerViewDelegate and add in this (remember to link the outlet in storyboard)

@property (weak, nonatomic) IBOutlet ADBannerView *banner;

3) In your ViewController.m

- (void)viewDidLoad
{
   self.banner.hidden = YES;
   //Add view controller as observer
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];
}

- (void)showBanner
{
    self.banner.hidden = NO;
}

- (void)hidesBanner
{
    self.banner.hidden = YES;
}

//Handle Notification
- (void)handleNotification:(NSNotification *)notification
{
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    }else if ([notification.name isEqualToString:@"showAd"]) {
        [self showBanner];
    }
}

4) In your Scene.m, in order to display the ad

e.g. player died and you want to show the banner

// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; //Sends message to viewcontroller to show ad.

5) In your Scene.m, in order to hide the ad

e.g. player restarted the game

// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to show ad.

*Advise hide the ad when it's not loaded.




回答2:


http://www.yokeharn.com/workflow-how-to-create-flappy-bird-game-in-3-days-day-3/ Scroll down to Hour 4: iAd. This is a good tutorial which helps you for 90% the last 10 % you can try yourself



来源:https://stackoverflow.com/questions/24736082/add-adbannerview-to-a-skscene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!