当我尝试在SpriteKit中加载iAd时,场景会重置并重新开始,同时还会运行已经在后台加载的场景。我创建了一个BOOL来帮助确定场景是否已经加载,如果已经加载,那么就不需要再加载它了。 iAd显示正常,但场景本身在屏幕上延伸。这是我的ViewController.m代码:
@implementation ViewController
{
BOOL _sceneLoaded;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setCanDisplayBannerAds:YES];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
SKView *skView = nil;
if (self.originalContentView) {
skView = (SKView *)self.originalContentView;
}else{
skView = (SKView *)self.view;
}
[skView setShowsDrawCount:NO];
[skView setShowsFPS:NO];
[skView setShowsNodeCount:NO];
if (_sceneLoaded == NO) {
SKScene *scene = [MainMenuScene sceneWithSize:skView.bounds.size];
[scene setScaleMode:SKSceneScaleModeFill];
[skView presentScene:scene];
_sceneLoaded = YES;
}
}
我不太确定我哪里出错了。我尝试在代码的不同区域调用if(_sceneLoaded == NO)语句,只是为了达到相同的结果。提前感谢您的帮助。
答案 0 :(得分:2)
下面的代码在iOS 7和8中进行了测试,效果很好。
在标题文件
中添加以下行#import <iAd/iAd.h>
@interface GameViewController : UIViewController<ADBannerViewDelegate>{
//iAd
ADBannerView *adView;
}
在实施文件.m中添加以下代码
#import "GameViewController.h"
#import "GameScene.h"
@implementation GameViewController
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView *skView;
if (self.originalContentView) {
skView = (SKView *)self.originalContentView;
}
if (!skView.scene) {
//[skView setShowsDrawCount:YES];
//[skView setShowsFPS:YES];
//[skView setShowsNodeCount:YES];
//Improve Performance
skView.ignoresSiblingOrder = YES;
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
[scene setScaleMode:SKSceneScaleModeFill];
[skView presentScene:scene];
}
}
- (void)awakeFromNib{
[super awakeFromNib];
CGRect screenRect = [[UIScreen mainScreen] bounds];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectMake(0, 0, screenRect.size.width, adView.frame.size.height);
adView.delegate=self;
[self.view addSubview:adView];
}
//iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[adView setAlpha:1.0];
NSLog(@"Show Ad");
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[adView setAlpha:0];
NSLog(@"Hide Ad");
}
//
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
-(BOOL)prefersStatusBarHidden{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
答案 1 :(得分:1)
如果您使用[self setCanDisplayBannerAds:YES],这会缩小您的视图以为iAd横幅腾出空间。我建议将ADBannerViewDelegate添加到视图控制器并将横幅设置为视图...查看本教程http://codewithchris.com/iad-tutorial/