【发布时间】:2023-03-16 11:19:01
【问题描述】:
控制台没有错误,似乎一切正常。但是bannerView_只是没有显示。
这是我的 AdMobObject.h
#import "AdMobObject.h"
#import "RootViewController.h"
#import "GADBannerView.h"
@class RootViewController;
@class GADBannerView;
@interface AdMobObject : UIViewController{
RootViewController * viewController;
GADBannerView * bannerView_;
}
+ (AdMobObject *) shared;
- (void) addAdMob;
- ( void) hideAdMob;
@end
这是我的 AdMobObject.mm
#import "AdMobObject.h"
#import "AppController.h"
#import "RootViewController.h"
#import "EAGLView.h"
#import "cocos2d.h"
@implementation AdMobObject
static AdMobObject* instance;
+(AdMobObject *) shared{
@synchronized(self){
if( instance == nil ){
instance = [[self alloc] init];
}
}
return instance;
}
- (void) addAdMob{
NSLog(@"----------addAdMob");
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
CGFloat screenWidth = screenRect.size.width;
viewController.view.frame = CGRectMake(0,0,screenWidth,screenHeight);
[bannerView_ setFrame:CGRectMake(0,
screenHeight-bannerView_.bounds.size.height,
//0,
bannerView_.bounds.size.width,
bannerView_.bounds.size.height)];
bannerView_.adUnitID = @"myadmobid";
bannerView_.rootViewController = viewController;
[viewController.view addSubview:bannerView_];
GADRequest *request = [GADRequest request];
// For testing
request.testDevices = [NSArray arrayWithObjects:@"mydeviceid", nil];
[bannerView_ loadRequest:request];
[viewController.view addSubview:bannerView_];
}
- (void) showAdMob{
}
- (void) hideAdMob{
[bannerView_ setHidden:YES];
}
@end
然后我有一个班级来管理它:
@interface MyGameCenterManager : NSObject
{
}
+(MyGameCenterManager *) shared;
+(void) addAdMob;
+(void) hideAdMob;
@end
这是类的实现:
@implementation MyGameCenterManager
static MyGameCenterManager *instance;
+(MyGameCenterManager*)shared{
@synchronized(self){
if(instance == nil){
instance = [[self alloc] init];
}
}
return instance;
}
+ (void) addAdMob
{
[[AdMobObject shared] addAdMob];
}
+ (void) hideAdMob{
[[AdMobObject shared] hideAdMob];
}
最后,我有 cpp 类:
包括“cocos2d.h”
#include "ScriptingCore.h"
namespace ls{
class GameCenterBridge: public cocos2d::CCObject{
public:
static cocos2d::CCScene* scene();
virtual bool init();
CREATE_FUNC(GameCenterBridge);
void addAdMob();
void hideAdMob();
};
}
我给我的班级打电话:
ls::GameCenterBridge * class= 新 ls::GameCenterBridge(); 类->addAdMob();
我可以看到控制台日志:
---------添加AdMob
这意味着它进入了 addAdMob 函数。并且没有其他错误。
但是横幅视图没有显示。
我让它显示的唯一方法是在didFinishLaunchingWithOptions 中将代码添加到AppController.mm。但我只是想知道为什么它不适用于我自己创建的课程。
【问题讨论】:
标签: c++ objective-c admob cocos2d-x