gpt4 book ai didi

ios - 如何在Cocos-SpriteBuilder中添加iAd

转载 作者:行者123 更新时间:2023-12-01 17:25:46 24 4
gpt4 key购买 nike

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

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

最佳答案

将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];
}

关于ios - 如何在Cocos-SpriteBuilder中添加iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21771126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com