Admob横幅没有从superview中删除

时间:2012-09-28 20:58:55

标签: objective-c cocos2d-iphone admob ios5.1

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

我没有得到admob banner如何在每个类中出现,实际上我还没有在Rootviewcontroller类中声明。任何人都可以建议我如何在cocos2d游戏中集成Admob,我希望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);
 }

`

3 个答案:

答案 0 :(得分: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  

}

答案 1 :(得分:1)

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

-(void)RemoveAds
{
    if (adBanner != nil)
    {
        [adBanner setRootViewController:nil];
        [adBanner removeFromSuperview];
        adBanner = nil;
    }
}

答案 2 :(得分:0)

因为在cocos2d中,你将拥有不同的场景类。

我的建议是为add banner创建一个单独的类,并使用静态方法为您完成工作。您必须在该类中保存添加横幅的引用,并使用静态方法,您可以在openglview中添加/删除它。

要删除,您只能这样做:[bannerView removeFromSuperview];