2012-09-28 38 views
1

我正在開發一款使用cocos2d框架的2D遊戲,在此遊戲中,我使用admob作廣告,在某些課程中並非在所有課程中,但admob橫幅在每個課程中都是可見的,也會崩潰。Admob橫幅未從超級視圖中移除

我不明白admob橫幅是如何出現在每個類中的,事實上我沒有在Rootviewcontroller類中聲明。任何一個可以建議我如何向AdMob在cocos2d遊戲整合,我想特別班AdMob聯播旗幟不是每一個類, 我使用最新的谷歌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 [email protected]"a15062384653c9e"; 
} 
else { 
    bannerView_.adUnitID [email protected]"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); 
} 

`

+0

用我更新的代碼在cocos2d 2.0,一切工作正常。 – Guru

回答

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 

} 
+0

我試圖使用你的代碼,但我得到一些錯誤,你可以請告訴我你已經使用 AppController *應用程序=(AppController *)[[UIApplication sharedApplication]委託];但它顯示錯誤使用未聲明的AppController和應用程序,請告訴我如何刪除這個錯誤,我在我的每個班級添加您的代碼。 – Gamer

+0

爲什麼你在admob中使用了「navController」,你能否解釋我一點,我的遊戲中沒有任何導航控制器。謝謝! – Gamer

+0

@Gamer,在cocos2d 2.0中,viewController被替換爲navController。我猜你使用cocos2D 1.0?使用App委託,使用此AppDelegate * app =(AppDelegate *)[[UIApplication sharedApplication]委託]; //app.viewController – Guru

0

由於在cocos2d,你將有場景不同類別。

我的建議是爲添加橫幅創建一個單獨的類,並有一個靜態方法爲您完成這項工作。您將不得不在該類中保存添加橫幅的引用,並且可以使用靜態方法將其添加到openglview或從openglview中移除它。

刪除你只會這樣做:[bannerView removeFromSuperview];

+0

感謝回覆我你是否有任何示例代碼,因爲由於我的代碼,遊戲也崩潰了。 – Gamer

+0

事故究竟是什麼?我想你會在循環中發佈釋放到uiviews的釋放實例。評論[SUB release]的行;然後再試一次。 –

+0

自動遊戲崩潰和更重要的事情一些橫幅不開放,我的意思是當我點擊橫幅,然後我沒有得到response.If我分開的類admob然後我怎麼可以打電話給另一個類我是noob在cocos2d和我是這個領域的新人。 – Gamer

1

完全修復(與iOS 8.1和AdMob 6.12.0測試)

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