问题描述
我使用的是SpriteBuilder(它与Cocos2d v3.0集成)。我建立了一个应用程序,现在我想在顶部放一个iAd,当我打电话时,它隐藏,当我告诉它。最简单的方法是什么?
I am using SpriteBuilder (which integrates with Cocos2d v3.0). I built an app and now I want to put in an iAd at the very top that pops up when I call it, and hides when I tell it to. What's the simplest way to do this?
记住我在Cocos2d中使用SpriteBuilder。只是因为我使用SpriteBuilder并不意味着我不使用Xcode 5。我也完全参与Xcode。
Keep in mind I am using SpriteBuilder with Cocos2d. And just because I am using SpriteBuilder does not mean I am not using Xcode 5 as well. I am fully involved in Xcode as well. SpriteBuilder does not write the code for me, I do that.
推荐答案
将iAd框架添加到您的依赖项。
Add iAd framework to your dependencies.
在您的游戏场景的头文件中,添加ADBannerViewDelegate,例如:
In your header file for your game scene, add the ADBannerViewDelegate, for instance:
@interface MainScene : CCNode <CCPhysicsCollisionDelegate, ADBannerViewDelegate>
在实施文件中,添加实例变量_bannerView:
In your implementation file, add the instance variable _bannerView:
@implementation MainScene {
ADBannerView *_bannerView;
}
最后,插入iAD代码(使用一些cocos2d调整)。这是我的一个游戏的纵向模式与顶部横幅的实现。没有隐藏方法,但它很容易实现。
And finally, insert the the iAD code (with some cocos2d tweaks). Here's my implementation for a game in portrait mode with a top banner. There's no hide method, but it's pretty easy to implement.
# 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];
}
这篇关于如何在Cocos-SpriteBuilder中添加iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!