Objective c Admob横幅未从superview中删除
我正在使用cocos2d框架开发一个2d游戏,在这个游戏中,我使用admob进行广告宣传,在某些类中,并非所有类中,但admob横幅在每个类中都可见,并且在一段时间后,游戏也会崩溃 我不明白admob banner是如何出现在每个类中的,事实上我还没有在Rootviewcontroller类中声明。有谁能建议我如何将Admob整合到cocos2d游戏中,我希望Admob旗帜出现在特定的类中,而不是出现在每个类中, 我正在使用最新的google admob sdk,我的代码如下: 提前谢谢 `Objective c Admob横幅未从superview中删除,objective-c,cocos2d-iphone,admob,ios5.1,Objective C,Cocos2d Iphone,Admob,Ios5.1,我正在使用cocos2d框架开发一个2d游戏,在这个游戏中,我使用admob进行广告宣传,在某些类中,并非所有类中,但admob横幅在每个类中都可见,并且在一段时间后,游戏也会崩溃 我不明白admob banner是如何出现在每个类中的,事实上我还没有在Rootviewcontroller类中声明。有谁能建议我如何将Admob整合到cocos2d游戏中,我希望Admob旗帜出现在特定的类中,而不是出现在每个类中, 我正在使用最新的google admob sdk,我的代码如下: 提前谢谢 ` `
`因为在cocos2d中,场景有不同的类 我的建议是为addbanner创建一个单独的类,并使用一个静态方法来完成这项工作。您必须在该类中保存添加横幅的引用,并且通过使用静态方法,您可以将其添加到openglview或从openglview中删除
对于删除,您只需执行:[bannerView removeFromSuperview] 因为在cocos2d中,场景有不同的类 我的建议是为addbanner创建一个单独的类,并使用一个静态方法来完成这项工作。您必须在该类中保存添加横幅的引用,并且通过使用静态方法,您可以将其添加到openglview或从openglview中删除
对于删除,您只需执行:[bannerView removeFromSuperview] 更新:请参考此处的一般答案: 我希望你已经找到了解决办法。若并没有,那个么下面是在cocos2D游戏中集成Admob的所有代码
#define ENABLE_ADMOB 1
//#define COCOS2D_2_0 1
@interface MyMainMenu : CCLayer
{
#ifdef ENABLE_ADMOB
GADBannerView *mBannerView;
#endif
}
@implementation MyMainMenu
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
#ifdef COCOS2D_2_0
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
#else
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
#endif
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
// 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.
//size
#ifdef COCOS2D_2_0
mBannerView.rootViewController = app.navController;
[app.navController.view addSubview:mBannerView];
#else
mBannerView.rootViewController = app.viewController;
[app.viewController.view addSubview:mBannerView];
#endif
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGSize AdSize = kGADAdSizeBanner.size;
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
#ifdef COCOS2D_2_0
frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
#else
frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
#endif
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.y = 0.0f;
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.y = 0.0f;
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
}
更新:请参阅此处的概括答案: 我希望你已经找到了解决办法。若并没有,那个么下面是在cocos2D游戏中集成Admob的所有代码
#define ENABLE_ADMOB 1
//#define COCOS2D_2_0 1
@interface MyMainMenu : CCLayer
{
#ifdef ENABLE_ADMOB
GADBannerView *mBannerView;
#endif
}
@implementation MyMainMenu
-(void)onEnter
{
[super onEnter];
#ifdef ENABLE_ADMOB
#ifdef COCOS2D_2_0
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
#else
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
#endif
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
// 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.
//size
#ifdef COCOS2D_2_0
mBannerView.rootViewController = app.navController;
[app.navController.view addSubview:mBannerView];
#else
mBannerView.rootViewController = app.viewController;
[app.viewController.view addSubview:mBannerView];
#endif
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGSize AdSize = kGADAdSizeBanner.size;
CGRect frame = mBannerView.frame;
frame.origin.y = -50.0f;
#ifdef COCOS2D_2_0
frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
#else
frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
#endif
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.y = 0.0f;
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.y = 0.0f;
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
}
完整修复(使用iOS 8.1和Admob 6.12.0进行测试)
完整修复(使用iOS 8.1和Admob 6.12.0进行测试)
谢谢回复我,你有没有任何示例代码,因为由于我的代码,游戏也会崩溃。崩溃到底是什么?我猜您正在向循环中取消分配的UIView实例发送发布。评论[分发布]行;然后再试一次。游戏自动崩溃,更重要的是一些横幅没有打开,我的意思是当我点击横幅时,我没有收到响应。如果我单独制作admob类,那么我如何调用另一个类我是cocos2d中的noob,我是这个领域的新手。感谢回复我,你有任何示例代码,因为我的代码,游戏也会崩溃。到底是什么崩溃?我猜您正在向循环中取消分配的UIView实例发送发布。评论[分发布]行;然后再试一次。游戏自动崩溃,更重要的是一些横幅没有打开,我的意思是,当我点击横幅时,我没有收到响应。如果我单独使用admob类,那么我如何调用另一个类我是cocos2d中的noob,我是这个领域的新手。我试图使用你的代码,但我遇到了一些错误。你能告诉我你使用了AppController app=(AppController)[UIApplication sharedApplication]代表];但是它显示了错误使用未声明的AppController和app请告诉我如何删除此错误我正在我的每个类中添加您的代码。为什么您在admob中使用“navController”,请您稍微解释一下,我的游戏中没有任何navigationcontroller。谢谢@玩家,在CoCoS2D2.0中,viewController被navController取代。我想你是在用CoCoS2D1.0吧?使用App delegate,使用此AppDelegate*App=(AppDelegate*)[[UIApplication sharedApplication]委托]//app.viewControllerIts提供错误:在对象类型Appdelegate上找不到属性“navcontroller”。以下代码提供错误:`mBannerView.rootViewController=app.navcontroller;[app.navController.view addSubview:mBannerView];frame.origin.x=(app.navController.view.bounds.size.width-AdSize.width)/2;`我应该在navControllerI的位置写些什么我试图使用你的代码,但我遇到了一些错误你能告诉我你使用了AppController app=(AppController)[[UIApplication sharedApplication]delegate];但是它显示了错误使用未声明的AppController和app请告诉我如何删除此错误我正在我的每个类中添加您的代码。为什么您在admob中使用“navController”,请您稍微解释一下,我的游戏中没有任何navigationcontroller。谢谢@玩家,在CoCoS2D2.0中,viewController被navController取代。我想你是在用CoCoS2D1.0吧?使用App delegate,使用此AppDelegate*App=(AppDelegate*)[[UIApplication sharedApplication]委托]//app.viewControllerIts提供错误:在对象类型Appdelegate上找不到属性“navcontroller”。以下代码提供错误:`mBannerView.rootViewController=app.navcontroller;[app.navController.view addSubview:mBannerView];frame.origin.x=(app.navController.view.bounds.size.width-AdSize.width)/2;`我应该在导航控制器的位置写些什么使用CoCoS2D2.0中更新的代码,一切正常。使用CoCoS2D2.0中更新的代码,一切正常。
-(void)RemoveAds
{
if (adBanner != nil)
{
[adBanner setRootViewController:nil];
[adBanner removeFromSuperview];
adBanner = nil;
}
}