【问题标题】:Admob banner integration in Cocos2d 2.0 / Admob banner in iphone gamesCocos2d 2.0 中的 Admob 横幅集成 / iphone 游戏中的 Admob 横幅
【发布时间】:2013-01-11 12:25:40
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios cocos2d-iphone admob


    【解决方案1】:

    这是我工作的 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;
    
             }];
        }
    }
    

    【讨论】:

    • 非常感谢,对我帮助很大!我将尝试立即实施。非常感谢!
    • @Guru - 在您的代码中,您指的是 AppController。这是什么课?这是你的 RootViewController 吗?我正在努力让它发挥作用。此外,navController 似乎不是 UIViewController 的一部分。你是说导航控制器吗?
    • 是的,在 cocos2d AppController = AppDelegate.你可以使用 UIViewController 代替 navController。
    • 仔细阅读后,我发现您的问题/答案适用于 cocos2D 版本 2。经过一些调整,我能够使其适用于版本 1.1。但是,即使我指定了 kBanner_Landscape_Top,广告仍然显示为 Portrait_top。此外,广告尺寸已正确设置为 kGADAdSizeSmartBannerLandscape,但由于它显示在设备的窄边,因此您无法与之交互。如果我将广告尺寸更改为 kGADAdSizeBanner,那么我可以与广告互动。但是,如果我打开广告然后关闭它,我的 cocos2d 视图会切换到纵向。有什么想法吗?
    • 我猜是 CGSize 的问题,打印 s 的值。在 cocos2d 1.0 中,只需使用视图控制器即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多