Cocos2d 2.0 中的 Admob 横幅集成/iphone 游戏中的 Admob 横幅横幅、游戏中、Cocos2d、iphone

2023-09-06 09:12:57 作者:心比薄荷凉

有人知道如何在 cocos 2d v2 中使用 admob,所有文档都基于视图根控制器,而 cocos2d 2 则以另一种方式运行.

Does anybody know how to make work admob in cocos 2d v2, all the documentation is based in a view root controller and cocos2d 2 go just in another way.

我找到的唯一文档是:Working-with-admob-and-cocos2d 但对于像我这样的新手来说有点可怜.如果有人能帮助我,我感激不尽!!

The only documentation I found was this: Working-with-admob-and-cocos2d but its a little poor for a newbie like me. If anyone can help me I appreciate too much!!

推荐答案

这是我工作的 admob cocos2d 代码:将 createAdmobAds、showBannerView、hideBannerView 和 dismissAdView 复制到你的类.

Here is my working admob cocos2d code: Copy createAdmobAds, showBannerView, hideBannerView and dismissAdView to your class.

这里是 Cocos2d 3.0 Admob 示例,对于 Cocos2d 2.0,请查看下方

Here is Cocos2d 3.0 Admob Sample , for Cocos2d 2.0 check below

#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;

         }];
    }
}
 
精彩推荐
图片推荐