我在我的2D游戏中实现了admob,我使用cocos2d来开发这个游戏,任何人都可以帮我设置位置admob横幅,我有纵向情绪的通用游戏,我想在底部显示横幅当我从一个视图导航到另一个视图时,要移除的设备和横幅当前它没有删除。 这是我的代码:
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
// AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
// 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:kGADAdSizeBanner];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
mBannerView.adUnitID =@"a15062384653c9e";
}
else
{
mBannerView.adUnitID =@"a15062392a0aa0a";
}
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
//size
mBannerView.rootViewController = viewController;
[viewController.view addSubview:mBannerView];
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGRect frame = mBannerView.frame;
frame.origin.y = (viewController.view.bounds.size.height) ;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
CGSize AdSize = kGADAdSizeLeaderboard.size;
frame.origin.y = 950;
}
else
{
CGSize AdSize = kGADAdSizeBanner.size;
frame.origin.y = 430;
}
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.y = 430;
mBannerView.frame = frame;
[UIView commitAnimations];
#endif
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.x = 0;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
#endif
}
答案 0 :(得分:1)
以下是我在iPhone游戏中使用Admob横幅的一般代码。
#import "GADBannerView.h"
typedef enum _bannerType
{
kBanner_Portrait_Top,
kBanner_Portrait_Bottom,
kBanner_Landscape_Top,
kBanner_Landscape_Bottom,
}CocosBannerType;
#define BANNER_TYPE kBanner_Landscape_Bottom //change this on need basis
@interface MyMainMenu : CCLayer
{
GADBannerView *mBannerView;
CocosBannerType mBannerType;
float on_x, on_y, off_x, off_y;
}
@implementation MyMainMenu
-(void)onEnter
{
[super onEnter];
[self createAdmobAds];
}
-(void)onExit
{
[self dismissAdView];
[super onExit];
}
-(void)createAdmobAds
{
mBannerType = BANNER_TYPE;
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.
if(mBannerType <= kBanner_Portrait_Bottom)
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
else
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;
off_x = 0.0f;
on_x = 0.0f;
switch (mBannerType)
{
case kBanner_Portrait_Top:
{
off_y = -frame.size.height;
on_y = 0.0f;
}
break;
case kBanner_Portrait_Bottom:
{
off_y = s.height;
on_y = s.height-frame.size.height;
}
break;
case kBanner_Landscape_Top:
{
off_y = -frame.size.height;
on_y = 0.0f;
}
break;
case kBanner_Landscape_Bottom:
{
off_y = s.height;
on_y = s.height-frame.size.height;
}
break;
default:
break;
}
frame.origin.y = off_y;
frame.origin.x = off_x;
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.x = on_x;
frame.origin.y = on_y;
mBannerView.frame = frame;
[UIView commitAnimations];
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = on_y;
frame.origin.x = on_x;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = off_y;
frame.origin.x = off_x;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = off_y;
frame.origin.x = off_x;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
}
答案 1 :(得分:-1)
如果有人想在他的游戏中使用admob,我也得到了解决方案和位置,他可以使用这段代码,谢谢@raj他帮我很多。
`
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
// AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
// 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.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard];
}
else {
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
}
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
mBannerView.adUnitID =@"a15062384653c9e";
}
else
{
mBannerView.adUnitID =@"a15062392a0aa0a";
}
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
//size
mBannerView.rootViewController = viewController;
[viewController.view addSubview:mBannerView];
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGRect frame = mBannerView.frame;
// frame.origin.y = (viewController.view.bounds.size.height) ;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
frame = mBannerView.frame;
frame.origin.y = 950;
frame.origin.x = 20;
}
else
{
frame = mBannerView.frame;
frame.origin.y = 430;
frame.origin.x = 0;
}
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
mBannerView.frame = frame;
[UIView commitAnimations];
#endif
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.x = 0;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
#endif
}
`