为什么AdWhirl在播放时没有显示广告?

时间:2012-06-01 15:30:06

标签: iphone xcode cocos2d-iphone iad adwhirl

根据Ray Wenderlich教程,我已经合并了最新的AdWhirl文件夹,删除了我不使用的适配器,使用iAd设置了帐户,上传了几个房屋横幅,配置了AdWhirl交付并实现了检索/显示代码在我的Cocos2d应用程序中。我的日志显示广告已成功检索但未显示在屏幕上。我想他们要么在屏幕外显示(没有男人的土地),要么隐藏在CCLayer背后。任何有关如何测试/解决此问题的建议都非常感谢。如果有帮助的话,这只是一个风景唯一的方向。

更新:对于Cocos2d 2.0版用户,RW教程已经过时了......请参阅下面的解决方案

这是我的AdWhirl代码:

Library.h

#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#import "RootViewController.h"
@interface Library : CCLayer <AdWhirlDelegate>{

这是教程所说但过时的

//RootViewController *viewController;

这是需要的

UINavigationController *viewController

AdWhirlView *adWhirlView;
enum GameStatePP _state; }
@property(nonatomic,retain) AdWhirlView *adWhirlView; 
@property(nonatomic) enum GameStatePP state;

Library.mm

@implementation Library
@synthesize state = _state, adWhirlView;

在Init

self.state = kGameStatePlaying;

然后是AdWhirl方法

- (void)adWhirlWillPresentFullScreenModal {
if (self.state == kGameStatePlaying) {
[[CCDirector sharedDirector] pause];} }

- (void)adWhirlDidDismissFullScreenModal {
if (self.state == kGameStatePaused)
    return;
else {
    self.state = kGameStatePlaying;
    [[CCDirector sharedDirector] resume]; }}

- (NSString *)adWhirlApplicationKey {
return @"myadWhirlKeyGoesHere"; }

- (UIViewController *)viewControllerForPresentingModalView {
return viewController;}

-(void)adjustAdSize {
[UIView beginAnimations:@"AdResize" context:nil];
[UIView setAnimationDuration:0.2];
CGSize adSize = [adWhirlView actualAdSize];
CGRect newFrame = adWhirlView.frame;
newFrame.size.height = adSize.height;
CGSize winSize = [CCDirector sharedDirector].winSize;
    newFrame.size.width = winSize.width;
newFrame.origin.x = (self.adWhirlView.bounds.size.width - adSize.width)/2;
    newFrame.origin.y = (winSize.height - adSize.height);
adWhirlView.frame = newFrame;
[UIView commitAnimations]; }

- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlVieww {
[adWhirlView rotateToOrientation:UIInterfaceOrientationLandscapeRight];
[self adjustAdSize];
NSLog(@"Library - adWhirlDidReceiveAd");
NSLog(@"%@",[adWhirlView mostRecentNetworkName]); }

-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlVieww usingBackup:(BOOL)yesOrNo {
NSLog(@"Library - adWhirlDidFailToReceiveAd");
NSLog(@"%@",[adWhirlView mostRecentNetworkName]); }

-(void)onEnter {

这是教程所说的,对于Cocos2d 2.0用户来说是错误的

//viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];

这是正确的方法

viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] navController];

self.adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.adWhirlView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[adWhirlView updateAdWhirlConfig];
CGSize adSize = [adWhirlView actualAdSize];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.adWhirlView.frame = CGRectMake((winSize.width/2)-(adSize.width/2),winSize.height-adSize.height,winSize.width,adSize.height);
self.adWhirlView.clipsToBounds = YES;
[viewController.view addSubview:adWhirlView];
[viewController.view bringSubviewToFront:adWhirlView];
[super onEnter]; }

-(void)onExit {
if (adWhirlView) {
    [adWhirlView removeFromSuperview];
    [adWhirlView replaceBannerViewWith:nil];
    [adWhirlView ignoreNewAdRequests];
    [adWhirlView setDelegate:nil];
    self.adWhirlView = nil;
}
[super onExit]; }

- (void) dealloc {
self.adWhirlView.delegate = nil;
self.adWhirlView = nil; }

在我的AppDelegate.h中

错误:

//RootViewController *viewController; 
//UIViewController *viewController;

这是正确的,在2.0 Cocos2d模板中提供,因此不需要添加,只需要使用:

UINavigationController *navController_;

因此不需要这些

//@property (nonatomic, retain) RootViewController *viewController;
//@property (nonatomic, retain) UIViewController *viewController;

AppDelegate.mm

@synthesize window=window_, navController=navController_, director=director_; 

不需要,因为我们将改为使用navController:

//viewController = viewController_;

DidFinishLaunchingWithOptions(对viewController的所有引用都被注释掉了)

//viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
//viewController.wantsFullScreenLayout = YES;
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
    pixelFormat:kEAGLColorFormatRGB565
            depthFormat:0
            preserveBackbuffer:NO
            sharegroup:nil   
            multiSampling:NO 
            numberOfSamples:0];
director_ = (CCDirectorIOS*) [CCDirector sharedDirector];
director_.wantsFullScreenLayout = YES;
[director_ setDisplayStats:NO];
[director_ setAnimationInterval:1.0/60];
[director_ setDelegate:self];
[director_ setProjection:kCCDirectorProjection2D];
[director_ setView:glView];
if( ! [director_ enableRetinaDisplay:YES] )CCLOG(@"Retina Display Not supported");
navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
navController_.navigationBarHidden = YES;

这是重要的一行,设置了navController

[window_ addSubview:navController_.view];
[glView setMultipleTouchEnabled:YES];
//[viewController_ setView:glView];
//[window_ addSubview:viewController_.view];
[window_ makeKeyAndVisible];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[CCFileUtils setiPadSuffix:@"-ipad"];
[CCFileUtils setRetinaDisplaySuffix:@"-hd"];
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[director_ pushScene: [TitleScene scene]]; 
return YES;

2 个答案:

答案 0 :(得分:0)

使用以下行指定viewController:

viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];

您确定您的appDelegate正在设置其viewController属性吗?换句话说,viewController是否为零?我会检查一下。

答案 1 :(得分:0)

如果仍有此问题,请使用以下代码。

你以前是

[viewController.view addSubview:adWhirlView];<br>
[viewController.view bringSubviewToFront:adWhirlView];

我的建议是

[[CCDirector sharedDirector].view addSubview:adWhirlView];<br>
[[CCDirector sharedDirector].view bringSubviewToFront:adWhirlView];

也许,之后你可以看到广告。