Native Ad - fan-ADN/nendSDK-cocos2d-x GitHub Wiki
Preparation
If you have not yet register app/ad placements nor downloaded SDK, please refer to following link.
SDK Implementation
Add SDK/Module to a project.
Display Native Ad
Implement the program to display native ads into the source file of Cocos2d-x.
For more details, please refer to The Contents of Native Ad
Layout of Native Ad
At first, define the area you would like to display ads as Node or the class which is subclass of Node.
The sample code which uses Layercolor is below.
auto adLayer = LayerColor::create(Color4B::GRAY, 300, 200);
Next, add Label/Sprite to the Node you have created.
Add Label if you would like to display texts, Sprite if you would like to display images.
The sample code to display advertising explicitly, ad text and ad image is below.
Implementation File
// Advertising Explicitly
auto prLabel = Label::create();
prLabel->setName("NendNativeAdPrLabel"); // original name (optional)
prLabel->setPosition(Point(..., ...));
adLayer->addChild(prLabel);
// Ad text
auto longTextLabel = Label::create();
longTextLabel->setName("NendNativeAdLongTextLabel"); // original name to identify ad elements
longTextLabel->setPosition(Point(..., ...));
adLayer->addChild(longTextLabel);
// Ad image
auto adImageSprite = Sprite::create();
adImageSprite->setName("NendNativeAdImageSprite"); // original name to identify ad elements
adImageSprite->setContentSize(Size(..., ...));
adImageSprite->setPosition(Point(..., ...));
adLayer->addChild(adImageSprite);
Each ad element need to be displayed in following restrictions.
- Display Advertising Explicitly
- Display at least one from either ad title/ad text/the name of the promotion
- Display ad image when it is ad space including ad image
- Scaling of the ad image has to be done within 30-150%
- Cutting out the ad image has to be done within 6% either way
- Prohibit to cutout Logo image
Loading Native Ad
Loading native ad by using NendNativeAdClient class
Header File
#include "NendNativeAdClient.h"
#include "NendNativeAdBinder.h"
class YourAppScene : public cocos2d::Layer
{
private:
// Remain the instance within a class
nend_module::NendNativeAdClient* _nativeAdClient;
nend_module::NendNativeAdBinder* _binder;
}
Implementation File
using namespace nend_module;
...
auto apiKey = "ApiKey published on the dashboard";
auto spotId = "SpotId published on the dashboard";
// Create NendNativeAdClient
_nativeAdClient = new NendNativeAdClient(apiKey, spotId);
// Connect the ad elements and Node to display those
_binder = new NendNativeAdBinder();
// Set the name for Label/Sprite
_binder->setPrText_Name("NendNativeAdPrLabel"); // Advertising Explicitly
_binder->setLongText_Name("NendNativeAdLongTextLabel"); // Ad text
_binder->setAdImage_Name("NendNativeAdImageSprite"); // Ad image
...
// Load the ad
_nativeAdClient->loadAd([=](NendNativeAd* nativeAd, NendNativeLoadResultCode resultCode, std::string errorMessage) {
if (NEND_SUCCESS_LOAD_AD == resultCode) {
// Example for immediately start drawing process after succeed loading
// Please call following function when you would like to start drawing process you’re your own timing.
// At the first argument, set the Node including each ad element which is created on the previous section.
nativeAd->renderAdViews(_adLayer, _binder, NAD_NATIVE_ADVERTISING_EXPLIICITY_PR);
} else {
// Fail Loading
...
}
});
YourAppScene:~YourAppScene()
{
// Release the instance when you do not need it any longer
delete _nativeAdClient;
delete _binder;
}
Auto Ad Reload
Function added from ver.2.1.0
Start Auto ad Reload
Reload ad at intervals of you set on enableAutoReload(const int interval, const std::function<void (nend_module::NendNativeAd*, NendNativeLoadResultCode, std::string)> &callback) method in NendNativeAdClient class.
const int interval of the first argument, you can set intervals by second.
Please set the intervals not less than 30 seconds.
_nativeAdClient->enableAutoReload(interval,[=](NendNativeAd* nativeAd, NendNativeLoadResultCode resultCode, std::string errorMessage){
if (nullptr != nativeAd) {
// Success Loading
} else {
// Fail Loading
}
});
Disable Auto Ad Reload
Disable auto ad reload at disableAutoReload() method of NendNativeAdClient class.
_nativeAdClient->disableAutoReload();
Register Ad Click Callback
You are able to receive the notification of ad click event by registering ad click callback.
nativeAd->setAdClickCallback([=](NendNativeAd *nativeAd, Node* node){
// when click occurs
CCLOG("Click ad.");
});