菜单

横幅广告

1. banner调用说明

1.1 API说明

类名:ATCocosSdk


API参数说明功能说明
loadBannerAdconst char * placementId, cocos2d::ValueMap parameters用于加载banner广告,placementId为广告位id;parameters可为第三方平台设置信息
showBannerAdInPostionconst char * placementId, std::string position在屏幕顶部或者底部展示指定广告位的banner广告,position参数:ATCocosSdk::KEY_TOP ATCocosSdk::KEY_BOTTOM
showBannerAdInPositionAndScenarioconst char * placementId, const char * position, const char * scenario在屏幕顶部或者底部展示指定广告位的banner广告,position参数:ATCocosSdk::KEY_TOP ATCocosSdk::KEY_BOTTOM scenario为广告展示场景,可从后台创建场景参数
showBannerAdconst char * placementId, cocos2d::ValueMap parameters展示指定广告位的banner广告,parameters为指定banner展示的x坐标、y坐标、w宽、h高
showBannerAdInScenarioconst char * placementId, cocos2d::ValueMap parameters, const char * scenario展示指定广告位的banner广告,parameters为指定banner展示的x坐标、y坐标、w宽、h高 scenario为广告展示场景,可从后台创建场景参数
removeBannerAdconst char * placementId移除banner广告
isBannerAdReadyconst char * placementId判断指定广告位的广告是否加载完成
checkBannerAdStatusconst char * placementId获取当前广告位的状态(Json字符串): 1、isLoading:是否正在加载 2、isReady:是否有广告缓存(与isBannerAdReady作用相同) 3、AdInfo:当前优先级最高的广告缓存信息
setBannerAdListenerATCocosBannerAdListener * listener, const char * placementId设置回调对象

1.2 Listener回调方法说明

接口:ATCocosBannerAdListener

API参数说明功能说明
onBannerAdLoadSuccessconst char * placementId广告加载完成
onBannerAdLoadFailedconst char * placementId, const char * errorString广告加载失败
errorString为加载失败的原因
onBannerClickedWithExtraconst char * placementId, const char * extra广告产生点击
extra包含当前广告的其他信息
onBannerShowWithExtraconst char * placementId, const char * extra广告展示
extra包含当前广告的其他信息
onBannerCloseWithExtraconst char * placementId, const char * extra广告关闭
extra包含当前广告的其他信息
onBannerAutoRefreshWithExtraconst char * placementId, const char * extra广告自动刷新
extra包含当前广告的其他信息
onBannerAutoRefreshFailWithExtraconst char * placementId, const char * errorString, const char * extra广告自动刷新失败,
errorString为失败的原因
extra包含当前广告的其他信息

1.3 调用示例

1、加载banner广告

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;

//v5.6.8新增,只针对Admob的自适应banner
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、展示banner广告

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、移除banner广告

 ATCocosSdk::removeBannerAd(bannerPlacementId);


最近修改: 2025-05-30Powered by