Menu

Banner Ads

1. Banner call description

1.1 API description

Class name: ATCocosSdk

APIParameter DescriptionFunction Description
loadBannerAdconst char * placementId, cocos2d::ValueMap parametersUsed to load banner ads
placementId is the ad slot id;
parameters can set information for the third-party platform
showBannerAdInPostionconst char * placementId, std::string positionDisplay banner ads in the specified advertising slot at the top or bottom of the screen, position parameter: ATCocosSdk::KEY_TOP ATCocosSdk::KEY_BOTTOM
showBannerAdInPositionAndScenarioconst char * placementId, const char * position, const char * scenarioDisplay banner ads in the specified advertising slot at the top or bottom of the screen, position parameter: ATCocosSdk::KEY_TOP ATCocosSdk::KEY_BOTTOM
Scenario is the ad display scene, and the scene parameters can be created from the background
showBannerAdconst char * placementId, cocos2d::ValueMap parametersDisplay banner ads in the specified ad slot
parameters are the specified banner displays x coordinate, y coordinate, w width, h height
showBannerAdInScenarioconst char * placementId, cocos2d::ValueMap parameters, const char * scenarioDisplay banner ads in the specified advertising slot.
Parameters are the x coordinate, y coordinate, w width, and h height of the specified banner display.
Scenario is the ad display scene, and the scene parameters can be created from the background
removeBannerAdconst char * placementIdRemove banner ads
isBannerAdReady const char * placementIdJudgment designation Whether the ads in the ad slot have been loaded
checkBannerAdStatusconst char * placementIdGet the status of the current ad slot (Json string):
1. isLoading : Whether it is loading
2. isReady: whether there is an ad cache (same function as isBannerAdReady)
3. AdInfo: the current highest priority ad cache information
setBannerAdListenerATCocosBannerAdListener * listener, const char * placementIdSet callback object

1.2 Listener callback method description

Interface: ATCocosBannerAdListener

APIParameter DescriptionFunction Description
onBannerAdLoadSuccessconst char * placementIdAd loading completed
onBannerAdLoadFailedconst char * placementId, const char * errorStringAd loading failed
errorString failed to load Reason
onBannerClickedWithExtraconst char * placementId, const char * extraThe ad clicks
extra contains other information about the current ad
onBannerShowWithExtraconst char * placementId, const char * extraAd display
extra contains other information about the current ad
onBannerCloseWithExtraconst char * placementId, const char * extraAd close
extra contains other information about the current ad
onBannerAutoRefreshWithExtraconst char * placementId, const char * extraThe ad auto refresh
extra contains additional information about the current ad
onBannerAutoRefreshFailWithExtraconst char * placementId, const char * errorString, const char * extraThe ad auto refresh fail
extra contains other information about the current ad
errorString failed to load Reason

1.3 Calling example

1. Load banner Advertisement

ATCocosSdk::setBannerAdListener(this, bannerPlacementId);

auto glView = Director::getInstance()->getOpenGLView();
auto frameSize = glView->getFrameSize();
int width = frameSize.width;
int height = frameSize.height;

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS   // IOS
    std::string widthStr = StringUtils::format("%d", (int)(width/ATCocosSdk::getScreenScale()));
    std::string heightStr = StringUtils::format("%d", (int)((width * 50 / 320)/ATCocosSdk::getScreenScale()));
#endif

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID   // Android
    std::string widthStr = StringUtils::format("%d", width);
    std::string heightStr = StringUtils::format("%d", width * 50 / 320);
#endif

ValueMap customDict;
customDict[ATCocosSdk::KEY_WIDTH] = widthStr;
customDict[ATCocosSdk::KEY_HEIGHT] = heightStr;

// Adaptive banner for Admob only
customDict[ATCocosSdk::KEY_INLINE_ADAPTIVE_WIDTH] = widthStr;
customDict[ATCocosSdk::KEY_INLINE_ADAPTIVE_ORIENTATION] = ATCocosSdk::INLINE_ADAPTIVE_ORIENTATION_CURRENT;
//customDict[ATCocosSdk::KEY_INLINE_ADAPTIVE_ORIENTATION] = ATCocosSdk::INLINE_ADAPTIVE_ORIENTATION_PORTRAIT;
//customDict[ATCocosSdk::KEY_INLINE_ADAPTIVE_ORIENTATION] = ATCocosSdk::INLINE_ADAPTIVE_ORIENTATION_LANDSCAPE;

ATCocosSdk::loadBannerAd(bannerPlacementId, customDict);

2. Display banner advertisement

if (ATCocosSdk::isBannerAdReady(bannerPlacementId)) {
    auto glView = Director::getInstance()->getOpenGLView();
    auto frameSize = glView->getFrameSize();
    int width = frameSize.width;
    int height = frameSize.height;

    ATCocosSdk::showBannerAdInPostion(bannerPlacementId, ATCocosSdk::KEY_BOTTOM);
} else {
    CCLOG("ATCocosSdk::isBannerAdReady is false");
}

3. Remove banner ads

 ATCocosSdk::removeBannerAd(bannerPlacementId);


Last modified: 2025-05-30Powered by