转载,在cocos2dx中使用iad
首先,膜拜不要理我总,他做过这个。各种膜拜
最新游戏BirdAndEgg 地址http://itunes.apple.com/us/app/birdandegg/id455201297?ls=1&mt=8
这里要涉及到c++和oc的交互,只有在.mm的文件类才能c++和oc混编,所以申明c++类的.h中不能出现oc的类
第一步到然是导入iad.framework,方法是在target属性里面build parse 中link Birnary with libraries。
第二步
方便起见我直接在FileOperation.h只里面添加东西了
static void addAd();//添加iad
static void deletAd();//删除iad
static void * view;//EGLView指针
static void * adView;//广告
static void * dl;//广告的delegate
static bool bannerIsVisible;//设置是否可见
static bool isIAD;//是否创建了广告
在在AppController.mm的代码中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 函数中
FileOperation::view= __glView;
在FileOpetation.mm文件中
void FileOperation::addAd(){
isIAD = false;
cocos2d::CCSize size = cocos2d::CCDirector::sharedDirector()->getWinSize();
NSString* versionString = [[UIDevice currentDevice] systemVersion];
int verNum = [versionString intValue];
if (verNum >= 4) {
isIAD = true;
// create and setup adview
dl=[[ADDL alloc] init];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
((ADBannerView *)adView).requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifier320x50 , nil];
#ifdef vertically
// ((ADBannerView *)adView).currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
#else
// ((ADBannerView *)adView).currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32; ((ADBannerView *)adView).transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));
#endif
((ADBannerView *)adView).center = OUT_POS;
((ADBannerView *)adView).delegate =(ADDL *)dl;
bannerIsVisible =false;
[(EAGLView *)view addSubview: (ADBannerView *)adView];
[(EAGLView *)view bringSubviewToFront:(ADBannerView *)adView];
((ADBannerView *)adView).frame = CGRectMake(0.0, 0.0, ((ADBannerView *)adView).frame.size.width, ((ADBannerView *)adView).frame.size.height); }
}
void FileOperation::deletAd(){
if(isIAD){
((ADBannerView *)adView).delegate = nil;
[((ADBannerView *)adView) removeFromSuperview];
[((ADBannerView *)adView) release];
adView = nil;
[((ADDL *)dl) relaease];
}
}
void * FileOperation::dl;
void * FileOperation::adView;
bool FileOperation::bannerIsVisible;
bool FileOperation::isIAD;
void * FileOperation::view;
最后是ADDL这个类了
//
// ADDL.h
// BirdAndEggLite
//
// Created by kinet on 8/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <iAd/iAd.h>
@interface ADDL :NSObject<ADBannerViewDelegate>
@end
// ADDL.m
// BirdAndEggLite
//
// Created by kinet on 8/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ADDL.h"
#import "FileOperation.h"
#include "cocos2d.h"
#define OUT_POS CGPointMake(-1024, -1024)
@implementation ADDL
- (id)init{
self = [super init];
if (self) {
}
return self;
}
-(void)relaease
{
[super dealloc];
}
- (void)dealloc
{
[super dealloc];
}
-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (FileOperation::bannerIsVisible) {
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
((ADBannerView *)FileOperation::adView).center = OUT_POS;
[UIView commitAnimations];
FileOperation::bannerIsVisible = false;
}
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration
{
//adView.frame = CGRectZero;
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
((ADBannerView *)FileOperation::adView).currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
((ADBannerView *)FileOperation::adView).frame = CGRectZero;
} else {
((ADBannerView *)FileOperation::adView).currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
((ADBannerView *)FileOperation::adView).frame = CGRectZero;
}
}
@end
使用方法是在类中FileOperation::addAd()添加广告
FileOperation::FileOperation::deletAd()删除广告