2013年05月26日

Cocos2dでAdMobを実装する

Cocos2dでAdMobを実装する方法。
画面上部に表示させる場合と、画面下部に表示させる場合の2通りを示す。

HogeLayer.h
#import
#import "cocos2d.h"
#import "GADBannerView.h"

@interface HogeLayer : CCLayer <GADBannerViewDelegate>{

}
@end


画面上部にAdMobを表示させる場合
HogeLayer.m
@implementation HogeLayer{
GADBannerView* _admobView;
}

-(void) onEnter
{

_admobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
_admobView.adUnitID = @"Your Publisher ID";
_admobView.rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[[[CCDirector sharedDirector] view] addSubview:_admobView];
_admobView.delegate = self;

GADRequest *request = [GADRequest request];
request.testing = NO;
[_admobView loadRequest:request];

}



画面下部にAdMobを表示させる場合
HogeLayer.m
@implementation HogeLayer{
GADBannerView* _admobView;
}

-(void) onEnter
{

CGRect rect = CGRectMake(0, winSize.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height);
_admobView = [[GADBannerView alloc] initWithFrame:rect];
_admobView.adUnitID = @"Your Publisher ID";
_admobView.rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[[[CCDirector sharedDirector] view] addSubview:_admobView];
_admobView.delegate = self;

GADRequest *request = [GADRequest request];
request.testing = NO;
[_admobView loadRequest:request];

}



画面上部に表示させる場合は、GADBannerViewをallocするときにinitWithAdSizeを指定するだけでよい。
画面下部に表示させる場合は、GADBannerViewをallocするときにinitWithFrameで表示領域を指定する。


参考記事:
http://narudesign.com/devlog/cocos2d-implement-admob/
http://blog.livedoor.jp/second_match/archives/51894540.html
http://tanukichi566.blog.fc2.com/blog-entry-61.html


posted by mobileDeveloper at 23:18 | Comment(0) | TrackBack(0) | AdMob | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック