how to show admob in different scene

Hi, I’ve added admob in my game.

in AppController.mm, I added following code:

    bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    [bannerView_ setFrame:CGRectMake(0,
                                    // screenHeight-bannerView_.bounds.size.height,,
                                     0,
                                     bannerView_.bounds.size.width,
                                     bannerView_.bounds.size.height)];
    bannerView_.adUnitID = @"myid";
    bannerView_.rootViewController = viewController;
    [viewController.view addSubview:bannerView_];
    [bannerView_ loadRequest:[GADRequest request]];

but this will result in displaying ad all the time.

so, just wondering, how to control of adding admob to different scene?

Got stuck on this for 2 days. Still have no idea.

I’ve implemented Admanager class in separate files to control it. But it doesn’t work at all.

so, here is what i’ve tried:

I created a AdBridge class:

namespace ls{
    
    
    class AdBridge: public cocos2d::CCObject{
        
    public:
        
        static cocos2d::CCScene* scene();
        virtual bool init();
        
        void showAddAtTop();
        void showAddAtBottom();
        
        
        
        
    };

}

here is the implementation:

void ls::AdBridge::showAddAtTop(){
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
    
    [AdBridgemanager showAddAtTop];
    
    
#endif
    
}

void ls::AdBridge::showAddAtBottom(){
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
    
    [AdBridgemanager showAddAtBottom];
    
    
#endif
    
}

bool ls::AdBridge::init(){
    bool bRef = false;
    do {
        cocos2d::CCLog("AdBridge init ...");
        bRef = true;
    } while (0);
    return bRef;
}

then I created AdBridgeManager:

@implementation AdBridgeManager

+ (void) showAddAtTop{
   
    if(view && size.width>0){
        
        [view setFrame:CGRectMake(0, -13, 320, 50)];
    }

    
}

+ (void) showAddAtBottom{
      if(view && size.width>0){
        
        [view setFrame:CGRectMake(0, size.height-50, 320, 50)];
    }
    
}

so, this is what I have done…

Then I bind it to JS.

then in the scene, I called :

 var admanager= new ls.AdBridgeManager();
        admanager.showAddAtBootm();

it suppose to work and show ad at bottom…

I did same thing for binding gamecenter and that worked.

But this ad banner always shows on top and never changed.

Is there anyone who can help me out this?

I guess i just used the wrong way to change the banner position.

Hello!

You can have a look at: http://www.cocos2d-x.org/forums/6/topics/46265?r=46487#message-46487
User ‘getarobo’ posted quite a nice code that shows how to add AdMob to his game and hide/display it whenever you want.
Maybe this can help you also with the bottom/top positioning problem!