【问题标题】:Why isn't AdWhirl displaying ads when it fetches okay?为什么 AdWhirl 在获取时不显示广告?
【发布时间】:2012-06-06 20:46:56
【问题描述】:

按照 Ray Wenderlich 教程,我已经合并了最新的 AdWhirl 文件夹,删除了我不使用的适配器,使用 iAd 设置了一个帐户,上传了一些房屋横幅,配置了 AdWhirl 交付并实施了检索/显示代码在我的 Cocos2d 应用程序中。我的日志显示广告已成功检索,但未出现在屏幕上。我在想它们要么显示在屏幕外(无人区),要么隐藏在 CCLayer 后面。非常感谢有关如何测试/解决此问题的任何建议。如果有帮助,这是一个仅限横向的方向。

更新:对于 Cocos2d 2.0 版本的用户来说,RW 教程已经过时了……解决方法见下文

这是我的 AdWhirl 代码:

图书馆.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;

图书馆.mm

@implementation Library
@synthesize state = _state, adWhirlView;

在初始化中

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_; 

不需要,因为我们将使用导航控制器:

//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;

这是重要的一行,设置导航控制器

[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;

【问题讨论】:

  • 您是否在某处设置了viewController 属性?如果不是,那将是一个问题。
  • 不确定你的设置是什么意思。上面的代码是与 CCLayer 中的 adWhirl(和视图控制器)相关的所有内容。是否需要在 init 中设置 self.view = view controller 之类的东西?

标签: iphone xcode cocos2d-iphone iad adwhirl


【解决方案1】:

你用这一行指定viewController:

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

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

【讨论】:

  • 我补充说: if (viewController == nil) {NSLog(@"viewController is nil");} 它确实记录了该消息。查看我的 AppDelegate,似乎我试图将 viewController 设置为 RootViewController,然后将其切换为 UIViewController 以使 AdWhirl 正常工作......似乎都没有正常工作。也许您可以为我指出设置 viewController 属性的正确方向...
  • 发布您正在执行该操作的方法(编辑您的原始问题,以便代码格式正确)。另外,您的方法 onEnter 是如何/何时被调用的?
  • 我已经添加了我的 AppDelegate 代码。我没有明确调用 onEnter 。我以为它是在场景启动时自动调用的......
  • 好的 - onEnter 不是 appDelegate 或 UIViewController 事件,因此它永远不会被调用(VC 生命周期事件涉及加载(进入内存)和出现(即显示)的状态:@987654326 @、viewWillAppearviewDidUnload 等)。您应该在应用程序完成时调用它。哦等等 - 这是 Cocoa2?啊 - 那么这也许是该 API 中的一个事件 - 但仅与 VC 相关(而不是 appDelegates)?需要检查的东西。
  • 根据 Cocos2d CCNode 类的引用,在第一次进入场景时(即转场开始时)调用 onEnter,在退出场景时(即转场结束时)调用 onExit。所以我认为它与设置正确的 viewController 有更多关系(即,它应该是 UIViewController 还是 RootViewController,有什么区别?,我是否错过了一步或在 AppDelegate 设置中出现了问题).. .
【解决方案2】:

如果你仍然有这个问题,请使用下面的代码。

你以前是

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

我的建议是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多