1

我正在使用 cocos2d 框架开发一个 2d 游戏,在这个游戏中我使用 admob 进行广告,在某些类中不是在所有类中,但 admob 横幅在每个类中都可见,并且在一段时间后游戏也会崩溃。

我不知道 admob 横幅是如何出现在每个类中的,事实上我没有在 Rootviewcontroller 类中声明。任何人都可以建议我如何将 Admob 集成到 cocos2d 游戏中,我希望 Admob 横幅在特定的类中而不是在每个类中,我使用的是最新的 google admob sdk,我的代码如下:

提前致谢

`

-(void)AdMob{
NSLog(@"ADMOB");


CGSize winSize = [[CCDirector sharedDirector]winSize];
// Create a view of the standard size at the bottom of the screen.


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

    bannerView_ = [[GADBannerView alloc]
                  initWithFrame:CGRectMake(size.width/2-364,
                                           size.height -
                                           GAD_SIZE_728x90.height,
                                           GAD_SIZE_728x90.width,
                                           GAD_SIZE_728x90.height)];

}
else { // It's an iPhone

    bannerView_ = [[GADBannerView alloc]
                  initWithFrame:CGRectMake(size.width/2-160,
                                           size.height -
                                           GAD_SIZE_320x50.height,
                                           GAD_SIZE_320x50.width,
                                           GAD_SIZE_320x50.height)];
}




if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    bannerView_.adUnitID =@"a15062384653c9e";
}
else {
    bannerView_.adUnitID =@"a15062392a0aa0a";
}

bannerView_.rootViewController = self;
[[[CCDirector sharedDirector]openGLView]addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];


GADRequest *request = [[GADRequest alloc] init];
request.testing =  [NSArray arrayWithObjects:
                  GAD_SIMULATOR_ID, nil];                              // Simulator

[bannerView_ loadRequest:request];
 }



   //best practice for removing the barnnerView_

-(void)removeSubviews{
 NSArray* subviews = [[CCDirector sharedDirector]openGLView].subviews;
 for (id SUB in subviews){
    [(UIView*)SUB removeFromSuperview];
    [SUB release];
 }
 NSLog(@"remove from view");
  }

    //this makes the refreshTimer count
-(void)targetMethod:(NSTimer *)theTimer{

//INCREASE OF THE TIMER AND SECONDS
elapsedTime++; 
seconds++;

//INCREASE OF THE MINUTOS EACH 60 SECONDS
if (seconds>=60) {
    seconds=0; minutes++;
    [self removeSubviews];
    [self AdMob];
  }
NSLog(@"TIME: %02d:%02d", minutes, seconds);
 }

`

4

3 回答 3

3

更新:在这里参考广义答案:Admob-banner-integration-in-cocos2d

我希望你已经得到了解决方案。如果没有,那么这里是 cocos2D 游戏中 Admob 集成的所有代码。

 #define ENABLE_ADMOB 1
 //#define COCOS2D_2_0 1

@interface MyMainMenu : CCLayer
{
    #ifdef ENABLE_ADMOB
    GADBannerView *mBannerView;
    #endif
}

@implementation MyMainMenu

-(void)onEnter
{
    [super onEnter];

    #ifdef ENABLE_ADMOB
       #ifdef COCOS2D_2_0
         AppController *app =  (AppController*)[[UIApplication sharedApplication] delegate];    
       #else 
         AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
       #endif
    mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    mBannerView.adUnitID = MY_BANNER_UNIT_ID;

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.


    //size


    #ifdef COCOS2D_2_0
    mBannerView.rootViewController = app.navController;
    [app.navController.view addSubview:mBannerView];
    #else 
    mBannerView.rootViewController = app.viewController;
    [app.viewController.view addSubview:mBannerView];
    #endif

    // Initiate a generic request to load it with an ad.
    [mBannerView loadRequest:[GADRequest request]];

    CGSize AdSize = kGADAdSizeBanner.size;

    CGRect frame = mBannerView.frame;
    frame.origin.y = -50.0f;

    #ifdef COCOS2D_2_0
    frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
    #else
    frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
    #endif

    mBannerView.frame = frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    frame = mBannerView.frame;
    frame.origin.y = 0.0f;

    mBannerView.frame = frame;
    [UIView commitAnimations];    

    #endif
}

-(void)showBannerView
{
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = 0.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)hideBannerView
{
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = -50.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = -50.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
             [mBannerView setDelegate:nil];
             [mBannerView removeFromSuperview];
             mBannerView = nil;

         }];
    }
#endif  

}
于 2012-09-29T17:17:55.460 回答
1

完全修复(使用 iOS 8.1 和 Admob 6.12.0 测试)

-(void)RemoveAds
{
    if (adBanner != nil)
    {
        [adBanner setRootViewController:nil];
        [adBanner removeFromSuperview];
        adBanner = nil;
    }
}
于 2014-11-05T08:03:08.793 回答
0

由于在 cocos2d 中,您将有不同的场景类。

我的建议是为添加横幅创建一个单独的类,并让一个静态方法为您完成这项工作。您必须在该类中保存对添加横幅的引用,并使用静态方法将其添加到 openglview 中/从中删除它。

对于删除你只会做:[bannerView removeFromSuperview];

于 2012-09-28T21:18:32.913 回答