//------------------------------------添加广告的方法
先加入以上这么些,然后在 AppController.mm里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples:0 ];
// Use RootViewController manage EAGLView
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = __glView;
//------------------- Add Admob
GADBannerView * bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView_.rootViewController = viewController;
bannerView_.adUnitID = @"a1517648ecd9c44";
GADRequest *request = [GADRequest request];
request.testing = NO;
// request.testDevices = [NSArray arrayWithObjects:
//
// GAD_SIMULATOR_ID,
//
// @"YOU IPAD IDF",
//
// nil];
//----------------------------
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:viewController];
}
[viewController.view addSubview: bannerView_];
[viewController.view bringSubviewToFront:bannerView_];
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden: YES];
[bannerView_ loadRequest: request];
cocos2d::CCApplication::sharedApplication()->run();
return YES;
}
广告就能显示了
广告显示之后,需要在不同的Scene切换广告的位置
实现方式为:
定义一个OC类:
#import
#import
@interface MyAdManager : NSObject
{
}
+(void)showAddAtTop;
+(void)showAddAtBottom;
@end
#import "MyAdManager.h"
#import "myoc.h"
@implementation MyAdManager
+ (void)showAddAtTop {
if (view && size.width > 0) {
[view setFrame:CGRectMake(0, -13, 320, 50)];
}
}
+ (void)showAddAtBottom {
if (view && size.width > 0) {
[view setFrame:CGRectMake(0, size.height - 50, 320, 50)];
}
}
@end
#import
#import
UIView* view;
CGSize size;
#endif
为了不修改需要添加广告Scene的文件后缀(方便Android编译)
添加一个中间类 AdBridge
#include
class AdBridge{
public:
static void showAdAtTop();
static void showAdAtBottom();
};
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "MyAdManager.h"
#endif
void AdBridge::showAdAtTop(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
[MyAdManager showAddAtTop];
#endif
}
void AdBridge::showAdAtBottom(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
[MyAdManager showAddAtBottom];
#endif
}
再将其加到到需要广告的Scene