问题描述
我正在使用 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?
请记住,我将 SpriteBuilder 与 Cocos2d 一起使用.仅仅因为我使用 SpriteBuilder 并不意味着我也没有使用 Xcode 5.我也完全参与了 Xcode.SpriteBuilder 不会为我编写代码,我会这样做.
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 调整).这是我对带有顶部横幅的纵向模式游戏的实现.没有 hide 方法,但很容易实现.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!