在游戏中我们经常会为游戏添加广告,但是有时候cocos2dx 添加admob广告会出现错误,那么我们怎样才能解决cocos2dx 添加admob广告错误呢?请看下面。

依照官方教程添加文件及其 frameWork后 发现运行报错 错误如下

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_ASIdentifierManager", referenced from:
objc-class-ref in libGoogleAdMobAds.a(GADIdentifierUtilities.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_SKStoreProductViewController", referenced from:
objc-class-ref in RevMobAds(RevMobStoreController.o)
"_SKStoreProductParameterITunesItemIdentifier", referenced from:
-[RevMobStoreController openStoreWithITunesItemId:] in RevMobAds(RevMobStoreController.o)
ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

最终通过添加 frameWork搞定  添加了 StoreKit.framework    AdSupport.framework  CoreData.framework .

运行继续报错

ld: duplicate symbol _OBJC_CLASS_$_AppDelegate in /Users/AbhilashReddy/Library/Developer/Xcode/DerivedData/santabantaa-bykvybsbvqshqshfqxdlsxiqhehc/Build/Intermediates/santabantaa.build/Debug-iphonesimulator/santabantaa.build/Objects-normal/i386/AppDelegate-C7A14BE43C5E8C81.o and /Users/AbhilashReddy/Library/Developer/Xcode/DerivedData/santabantaa-bykvybsbvqshqshfqxdlsxiqhehc/Build/Intermediates/santabantaa.build/Debug-iphonesimulator/santabantaa.build/Objects-normal/i386/AppDelegate-A37C3709371E4892.o for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

原来很是2的把demo也给弄进来了,删除demo的那个文件夹Add-ons 就好了
再次报错 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x95c17d0'

解决方案 add -ObjC to the Other Linker Flags of your application target's build setting。

加了个广告 他妹的把所有能碰到的问题都碰到了 真是技术不纯熟害死人啊。~~~

PS:要弄个全屏的Admob 看了下SDK 似乎没有 就自己动手写

[[GADBannerView alloc] initWithFrame:self.view.bounds];  结果报错 errors = "Ad size will not fit on screen"

但是改为 [[GADBannerView alloc] initWithFrame:CGRectMake(0,0,320,480)];  就可以。 但是同时写了下半屏的CGRect,发现半屏的 frame 显示的不完全,左右被压缩得很厉害,造成了只显示了一个icon和一个按钮。不知道怎么解决。

cocos2dx 添加admob广告错误

//------------------------------------添加广告的方法
先加入以上这么些,然后在 AppController.mm里面
  1. [plain] view plaincopy
  2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  3.  // Override point for customization after application launch.
  4. // Add the view controller's view to the window and display.
  5. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
  6. EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
  7. pixelFormat: kEAGLColorFormatRGBA8
  8. depthFormat: GL_DEPTH_COMPONENT16
  9. preserveBackbuffer: NO
  10. sharegroup: nil
  11. multiSampling: NO
  12. numberOfSamples:0 ];
  13.  // Use RootViewController manage EAGLView
  14. viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
  15. viewController.wantsFullScreenLayout = YES;
  16. viewController.view = __glView;
  17.  //------------------- Add Admob
  18. GADBannerView * bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
  19. bannerView_.rootViewController = viewController;
  20. bannerView_.adUnitID = @"a1517648ecd9c44";
  21. GADRequest *request = [GADRequest request];
  22. request.testing = NO;
  23.  //    request.testDevices = [NSArray arrayWithObjects:
  24. //
  25. //                           GAD_SIMULATOR_ID,
  26. //
  27. //                           @"YOU IPAD IDF",
  28. //
  29. //                           nil];
  30. //----------------------------
  31. // Set RootViewController to window
  32. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
  33. {
  34.  // warning: addSubView doesn't work on iOS6
  35. [window addSubview: viewController.view];
  36. }
  37. else
  38. {
  39.  // use this method on ios6
  40. [window setRootViewController:viewController];
  41. }
  42. [viewController.view addSubview: bannerView_];
  43. [viewController.view bringSubviewToFront:bannerView_];
  44. [window makeKeyAndVisible];
  45. [[UIApplication sharedApplication] setStatusBarHidden: YES];
  46. [bannerView_ loadRequest: request];
  47. cocos2d::CCApplication::sharedApplication()->run();
  48. return YES;
  49. }
广告就能显示了
广告显示之后,需要在不同的Scene切换广告的位置
实现方式为:

定义一个OC类:

  1. [plain] view plaincopy
  2. #import <Foundation/Foundation.h>
  3. #import <UIKit/UIKit.h>
  4. @interface MyAdManager : NSObject
  5. {
  6. }
  7. +(void)showAddAtTop;
  8. +(void)showAddAtBottom;
  9. @end
  10. [plain] view plaincopy
  11. #import "MyAdManager.h"
  12. #import "myoc.h"
  13. @implementation MyAdManager
  14. + (void)showAddAtTop {
  15. if (view && size.width > 0) {
  16. [view setFrame:CGRectMake(0, -13, 320, 50)];
  17. }
  18. }
  19. + (void)showAddAtBottom {
  20. if (view && size.width > 0) {
  21. [view setFrame:CGRectMake(0, size.height - 50, 320, 50)];
  22. }
  23. }
  24. @end
  25. 添加一个全局的 UIView* view,和CGSize size放置到myoc.h
  26. [plain] view plaincopy
  27. #import <Foundation/Foundation.h>
  28. #import <UIKit/UIKit.h>
  29. UIView* view;
  30. CGSize size;
  31. #endif
由于OC和c++进行混编时,文件后缀需要改成.mm
为了不修改需要添加广告Scene的文件后缀(方便Android编译)
添加一个中间类  AdBridge
  1. [plain] view plaincopy
  2. #include <iostream>
  3. class AdBridge{
  4. public:
  5. static void showAdAtTop();
  6. static void showAdAtBottom();
  7. };
  8. [plain] view plaincopy
  9. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  10. #include "MyAdManager.h"
  11. #endif
  12. void AdBridge::showAdAtTop(){
  13. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  14. [MyAdManager showAddAtTop];
  15. #endif
  16. }
  17. void AdBridge::showAdAtBottom(){
  18. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  19. [MyAdManager showAddAtBottom];
  20. #endif
  21. }
然后将AdBridge.cpp后缀改为mm
再将其加到到需要广告的Scene