我知道至少有一个问题询问如何将iAd集成到sprite kit游戏中,但这不是我想要的。我正在寻找一个如何做的肖像版本。关于如何做到这一点似乎绝对有0个在线教程,所以我来到这里。有人可以告诉我如何在Sprite Kit游戏中简单地启用iAd吗?我已启用canDisplayBannerAds
并已将originalContentView
属性用于我的UIView,但我一直遇到崩溃说
* 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:' - [ViewController originalContentView]:无法识别的选择器发送到实例0x10a0a7410'
任何帮助将不胜感激,这是我的视图控制器中的代码
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
答案 0 :(得分:10)
对于那些不了解的人,以及那些未来参考的人,以下是我对我的Sprite Kit游戏的处理方式。
我在Xcode中使用SpriteKit模板创建了我的游戏项目,然后进入项目设置:
在" Link Binary With Libraries"部分,只需确保点击+按钮,然后添加iAd框架。
执行此操作后,转到您的视图控制器以获取Sprite Kit游戏,然后输入:
// at the top
#import <iAd/iAd.h>
// do this in .m, above @implementation
@interface YourViewControllerClassName ()
@property (nonatomic, strong) ADBannerView *banner;
@end
// after the implementation line
// if you're needing to do it horizontally, do:
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero];
self.banner.delegate = self;
[self.banner sizeToFit];
self.canDisplayBannerAds = YES;
SKView *view = (SKView *)self.originalContentView;
SKScene *scene = [[YourScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
[view presentScene:scene];
}
如果您只是在正常的肖像中进行iAd,您可以执行上述代码,但您也可以使用 - (void)viewDidLoad代替......
现在是iAd的委托方法出现......
转到@implementation行上方的代码,然后进行编辑
// do this in .m, above @implementation
@interface YourViewControllerClassName () <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *banner;
@end
现在,进入实施线,然后输入:
// NOTE: THIS CODE CAME FROM APPLE MOSTLY
// I DID EDIT IT, BUT THE CREDIT GOES TO APPLE'S DOCUMENTATION
// ON IAD
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (banner.isBannerLoaded) {
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!banner.isBannerLoaded) {
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
}
}
这就是iAd在SpriteKit游戏中实际工作所需的全部内容。我希望我能帮助那些读这篇文章的人。