2013-01-11 156 views

回答

17

这里是我的工作admob cocos2d代码:复制createAdmobAds,showBannerView,hideBannerView和dismissAdView到你的班级。

这里是Cocos2d 3.0 Admob Sample,为的cocos2d 2.0以下检查

#import "GADBannerView.h" 

typedef enum _bannerType 
{ 
    kBanner_Portrait_Top, 
    kBanner_Portrait_Bottom, 
    kBanner_Landscape_Top, 
    kBanner_Landscape_Bottom, 
}CocosBannerType; 

#define BANNER_TYPE kBanner_Landscape_Bottom //change this on need basis 

@interface MyMainMenu : CCLayer 
{ 
    GADBannerView *mBannerView; 
    CocosBannerType mBannerType; 
    float on_x, on_y, off_x, off_y; 
} 

@implementation MyMainMenu 


-(void)onEnter 
{ 
    [super onEnter]; 
    [self createAdmobAds]; 
} 

-(void)onExit 
{ 
    [self dismissAdView]; 
    [super onExit]; 
} 

-(void)createAdmobAds 
{ 
    mBannerType = BANNER_TYPE; 

    AppController *app = (AppController*)[[UIApplication sharedApplication] delegate]; 
    // Create a view of the standard size at the bottom of the screen. 
    // Available AdSize constants are explained in GADAdSize.h. 

    if(mBannerType <= kBanner_Portrait_Bottom) 
     mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait]; 
    else 
     mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape]; 

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

    mBannerView.rootViewController = app.navController; 
    [app.navController.view addSubview:mBannerView]; 

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

    CGSize s = [[CCDirector sharedDirector] winSize]; 

    CGRect frame = mBannerView.frame; 

    off_x = 0.0f; 
    on_x = 0.0f; 

    switch (mBannerType) 
    { 
     case kBanner_Portrait_Top: 
     { 
      off_y = -frame.size.height; 
      on_y = 0.0f; 
     } 
      break; 
     case kBanner_Portrait_Bottom: 
     { 
      off_y = s.height; 
      on_y = s.height-frame.size.height; 
     } 
      break; 
     case kBanner_Landscape_Top: 
     { 
      off_y = -frame.size.height; 
      on_y = 0.0f; 
     } 
      break; 
     case kBanner_Landscape_Bottom: 
     { 
      off_y = s.height; 
      on_y = s.height-frame.size.height; 
     } 
      break; 

     default: 
      break; 
    } 

    frame.origin.y = off_y; 
    frame.origin.x = off_x; 

    mBannerView.frame = frame; 

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

    frame = mBannerView.frame; 
    frame.origin.x = on_x; 
    frame.origin.y = on_y; 


    mBannerView.frame = frame; 
    [UIView commitAnimations]; 
} 


-(void)showBannerView 
{ 
    if (mBannerView) 
    { 
     [UIView animateWithDuration:0.5 
           delay:0.1 
          options: UIViewAnimationCurveEaseOut 
         animations:^ 
     { 
      CGRect frame = mBannerView.frame; 
      frame.origin.y = on_y; 
      frame.origin.x = on_x; 

      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 = off_y; 
      frame.origin.x = off_x; 
     } 
         completion:^(BOOL finished) 
     { 
     }]; 
    } 

} 

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

     }]; 
    } 
} 
+0

非常感谢你,那我有很大帮助!我会尽力现在执行。 Thnaks很多! – user1832082

+0

它工作完美!漂亮而干净的代码! thnks! – user1832082

+0

@ user1832082,欢迎 – Guru