精灵套件游戏中的 iAd [英] iAd in sprite kit game
问题描述
我知道至少有一个问题询问如何将 iAd 集成到精灵套件游戏中,但这不是我想要的.我正在寻找如何做到这一点的肖像版本.关于如何做到这一点,网上似乎绝对有 0 个教程,所以我来到了这里.有人可以告诉我如何在 Sprite Kit 游戏中简单地启用 iAd 吗?我已启用 canDisplayBannerAds
并为我的 UIView 使用了 originalContentView
属性,但我不断收到提示
I know there is at least one question asking how to integrate iAd into a sprite kit game, but that is not what I am looking for. I am looking for a portrait version of how to do it. There seem to be absolutely 0 tutorials online as to how to do this, so I came here. Can someone please tell me how to simply enable iAd in a Sprite Kit game? I have enabled canDisplayBannerAds
and have used the originalContentView
property for my UIView, but I keep getting a crash that says
* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[ViewController originalContentView]:无法识别的选择器发送到实例 0x10a0a7410"
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController originalContentView]: unrecognized selector sent to instance 0x10a0a7410'
任何帮助将不胜感激,这是我在视图控制器中的代码
any help would be appreciated, and here is my code in my view controller
- (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];
}
推荐答案
对于那些不明白的人,以及那些供将来参考的人,以下是我对 Sprite Kit 游戏的处理方式.
For those that don't understand, and those that are for future references, here is how I did this to my Sprite Kit game.
我在 Xcode 中使用 SpriteKit 模板创建了我的游戏项目,然后进入项目设置:
I had my game project created using the SpriteKit template in Xcode, then went to the project settings as so:
在Link Binary With Libraries"部分,只需确保点击 + 按钮,然后添加 iAd 框架.
In the "Link Binary With Libraries" section, just make sure to hit the + button, and add the iAd framework.
完成此操作后,转到您的 Sprite Kit 游戏的视图控制器,然后输入以下内容:
After doing that, go to your view controller for your Sprite Kit game, and then type this:
// 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...
If you're doing the iAd in just a normal, portrait, you can do the above code, but you can also just use - (void) viewDidLoad instead...
现在是 iAd 出现的委托方法...
Now is the delegate methods for the iAd to appear...
转到@implementation 行上方的代码,然后编辑它
Go to the code above the @implementation line, and edit it
// do this in .m, above @implementation
@interface YourViewControllerClassName () <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *banner;
@end
现在,进入实施行,输入:
Now, go under the implementation line, and enter this:
// 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 游戏中实际工作所需的全部内容.我希望我能帮助那些将要阅读这篇文章的人.
That is all that is required from the iAd to actually work in a SpriteKit game. I hope that I helped those that are going to read this.
这篇关于精灵套件游戏中的 iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!