【问题标题】:Is Admob's new SDK compatible with cocos2d?Admob 的新 SDK 是否兼容 cocos2d?
【发布时间】:2013-01-19 02:12:00
【问题描述】:

Admob 发布了新的 SDK v6.2.1,过去几天我一直在尝试实现它,但没有成功。 SDK 中的谷歌分析插件 main.m 有问题:

Error 1: Stray '@' in program
Error 2: 'autoreleasepool' undeclared (first use in this function)
Error 3: Expected ';' before '{' token

main.m file:

//
// main.m
// CuteAnimals
//
// Copyright 2012 Google, Inc. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[]) {
    @autoreleasepool {
       return UIApplicationMain(argc, argv, nil,
             NSStringFromClass([AppDelegate class]));
    }
}

我已经链接了所有需要的库:

AudioToolbox.framework
MessageUI.framework
AVFoundation.framework
StoreKit.framework
iAd.framework
SystemConfiguration.framework
QuartzCore.framework
OpenGLES.framework
OpenAL.framework
UIKit.framework
Foundation.framework
CoreGraphics.framework
libGoogleAdMobAds.a
libGoogleAnalytics.a
libGoogleAnalytics_debug.a

我什至还没有包含或实施 GAdbannerView。该项目甚至无法使用包含的 SDK 进行编译。每当我删除包含(DoubleClick、GoogleAnalyticsiOS_2.0beta3、Mediation、Search)的附加组件文件夹时,项目都会编译。 但是,如果我尝试实现 GADBannerView(没有插件文件夹),则会出现 Mach-O 链接器错误,因为缺少分析插件文件。

cocos2d v1.X
Xcode v4.5.2

这里有什么我遗漏的吗?

* 编辑 *
似乎我包含了 SDK 下载中提供的所有内容,其中包括一个示例项目。仅包含 GAD 类、libGoogleAdMobAds.a、README.txt 和一个附加库 (AdSupport.framework) 后,它编译得很好。希望对您有所帮助。

【问题讨论】:

    标签: xcode cocos2d-iphone admob adbannerview


    【解决方案1】:

    您可以在 cocos2d 游戏中使用 admob。这是工作代码。

    @interface MyMainMenu : CCLayer
    {
        GADBannerView *mBannerView;
    }
    
    @implementation MyMainMenu
    
    
    -(void)onEnter
    {
        [super onEnter];
    
        [self createAdmobAds];
    
    }
    
    -(void)createAdmobAds
    {
        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.
        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;
        frame.origin.y = s.height;
    
        frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
    
        mBannerView.frame = frame;
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
        frame = mBannerView.frame;
        frame.origin.y = s.height - frame.size.height;
        frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
    
        mBannerView.frame = frame;
        [UIView commitAnimations];    
    
    }
    
    
    -(void)showBannerView
    {
        if (mBannerView) 
        {
            [UIView animateWithDuration:0.5
                                  delay:0.1
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 CGSize s = [[CCDirector sharedDirector] winSize];
    
                 CGRect frame = mBannerView.frame;
                 frame.origin.y = s.height - frame.size.height;
                 frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
    
                 mBannerView.frame = frame;
             } 
                             completion:^(BOOL finished)
             {
             }];
        }
    
    }
    
    
    -(void)hideBannerView
    {
        if (mBannerView) 
        {
            [UIView animateWithDuration:0.5
                                  delay:0.1
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 CGSize s = [[CCDirector sharedDirector] winSize];
    
                 CGRect frame = mBannerView.frame;
                 frame.origin.y = frame.origin.y +  frame.size.height;
                 frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
             } 
                             completion:^(BOOL finished)
             {
             }];
        }
    
    }
    
    
    -(void)dismissAdView
    {
        if (mBannerView) 
        {
            [UIView animateWithDuration:0.5
                                  delay:0.1
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 CGSize s = [[CCDirector sharedDirector] winSize];
    
                 CGRect frame = mBannerView.frame;
                 frame.origin.y = frame.origin.y + frame.size.height ;
                 frame.origin.x = (s.width/2.0f - frame.size.width/2.0f);
                 mBannerView.frame = frame;
             } 
                             completion:^(BOOL finished)
             {
                 [mBannerView setDelegate:nil];
                 [mBannerView removeFromSuperview];
                 mBannerView = nil;
    
             }];
        }
    
    }
    

    【讨论】:

    • 谢谢,但这不是问题所在。如果没有代码实现,它甚至无法编译。仅包括 SDK 就给了我错误。我想通了,并很快提供了答案..
    【解决方案2】:

    似乎我包含了 SDK 下载中提供的所有内容,其中包括一个示例项目。仅包含 GAD 类、libGoogleAdMobAds.a、README.txt 和一个附加库 (AdSupport.framework) 后,它编译得很好。希望有帮助。

    【讨论】:

      【解决方案3】:

      我相信@autoreleasepool 仅在 ARC 下有效。如果您的项目未使用 ARC,则可以使用 -fobjc-arc 编译器标志仅为该文件启用它。 (或者,因为 main.m 并没有真正做任何特别的事情,所以坚持使用你拥有的那个。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多