1

我正在使用 SpriteBuilder(与 Cocos2d v3.0 集成)。我构建了一个应用程序,现在我想在最顶部放置一个 iAd,当我调用它时它会弹出,当我告诉它时它会隐藏。最简单的方法是什么?

请记住,我将 SpriteBuilder 与 Cocos2d 一起使用。仅仅因为我使用 SpriteBuilder 并不意味着我也没有使用 Xcode 5。我也完全参与了 Xcode。SpriteBuilder 不会为我编写代码,我会这样做。

4

1 回答 1

8

将 iAd 框架添加到您的依赖项中。

在游戏场景的头文件中,添加 ADBannerViewDelegate,例如:

@interface MainScene : CCNode <CCPhysicsCollisionDelegate, ADBannerViewDelegate>

在您的实现文件中,添加实例变量 _bannerView:

@implementation MainScene {
    ADBannerView *_bannerView;
}    

最后,插入 iAD 代码(通过一些 cocos2d 调整)。这是我对带有顶部横幅的纵向模式游戏的实现。没有隐藏方法,但它很容易实现。

# pragma mark - iAd code

-(id)init
{
    if( (self= [super init]) )
    {
        // On iOS 6 ADBannerView introduces a new initializer, use it when available.
        if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
            _adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];

        } else {
            _adView = [[ADBannerView alloc] init];
        }
        _adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
        _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        [[[CCDirector sharedDirector]view]addSubview:_adView];
        [_adView setBackgroundColor:[UIColor clearColor]];
        [[[CCDirector sharedDirector]view]addSubview:_adView];
        _adView.delegate = self;
    }
    [self layoutAnimated:YES];
    return self;
}


- (void)layoutAnimated:(BOOL)animated
{
    // As of iOS 6.0, the banner will automatically resize itself based on its width.
    // To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
    CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
    if (contentFrame.size.width < contentFrame.size.height) {
        _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    } else {
        _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }

    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {
        contentFrame.size.height -= _bannerView.frame.size.height;
        bannerFrame.origin.y = contentFrame.size.height;
    } else {
        bannerFrame.origin.y = contentFrame.size.height;
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _bannerView.frame = bannerFrame;
    }];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [self layoutAnimated:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [self layoutAnimated:YES];
}
于 2014-02-15T11:41:24.670 回答