1

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

不需要,因为我们将使用 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;
4

2 回答 2

0

如果您仍然遇到此问题,请使用下面的代码。

你以前是

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

我的建议是

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

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

于 2013-05-03T13:40:16.850 回答
0

您可以使用以下行指定 viewController:

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

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

于 2012-06-03T21:04:44.983 回答