4) Cocos2dx

Before proceeding this chapter, be sure that you already finished SDK settings. If not, check  [6) Cocos2dx Settings] first.

[Incentive Interstitial Ad]

To display incentive interstitial ads in your app, simply write down two lines of code as follows.

Incentive Ad Sample

#include "TnkAdPlugin.h"


void HelloWorld::buttonTapped() 

{

    //   ...


    TnkAdPlugin::prepareInterstitialAd(TNK_PPI);

    TnkAdPlugin::showInterstitialAd();

}



[Non-Incentive Intestitial Ad]

To display non-incentive interstitial ads in your app, simply write down two lines of code as follows.

Non-Incentive Ad Sample

#include "TnkAdPlugin.h"


void HelloWorld::buttonTapped() 

{

    //   ...


    TnkAdPlugin::prepareInterstitialAd(TNK_CPC);

    TnkAdPlugin::showInterstitialAd();

}


[EventHandler]

To catch events that occurs when an interstitial ad is loaded or when users close interstitial ads, use TnkAdEventHandler objects.
EventHandler is for getting return values from async invocations or getting events related to interstitial popup view.
See B. EventHandler for details about EventHandler.

Callbacks related to interstitial Ads are listed as follows.
  • onClose(int handleId, int type) : Called when an interstitial Ad is closed. The reason why it is closed is passed to type parameter.
    • TNK_CLOSE_SIMPLE (0) : User closed ad view by clicking close button.
    • TNK_CLOSE_CLICK (1) : User clicked and ad view is closed.
    • TNK_CLOSE_EXIT (2) : User clicked an exist button on the view. In this case, you have to implements app-terminating logic in this callback.
  • onFailure(int handleId, int errCode) : Called when interstitial ad cannot be loaded. Error code is passed to errCode parameter.
    • TNK_FAIL_NO_AD (-1) : No ad from server.
    • TNK_FAIL_NO_IMAGE (-2) : No ad image found.
    • TNK_FAIL_TIIMEOUT (-3) : Ad not loaded in 5 secs after showInterstitialAd() invoked. In this case, ad is not displayed.
    • TNK_FAIL_CANCELED (-3) : Ad loading is canceled by the frequency of ad configured at Tnk site.
    • TNK_FAIL_SYSTEM (-9) :  Server or network failure.
  • onLoad(int handleId) : Called when an interstitial ad is successfully loaded from server. If showInterstitialAd() method is invoked before ad is loaded, then on arriving ad from server, onLoad() is not called. Instead. onShow() is called and ad is displayed immediately.
  • onShow(int handleId) : Called when ad is displayed on screen.
Following sample code shows how developers can control displaying interstitial ads using TnkAdEventHandler.

TnkAdEventHandler Example

// HelloWorldScene.h


class HelloWorld : public cocos2d::CCLayerpublic TnkAdEventHandler

{

private:

    //  ...

public:

    // add TnkAdEventHandler methods

    virtual void onLoad(int handlerId);


};


// HelloWorldScene.cpp


bool HelloWorld::init()

{

    // ...


    // Set EventHandler object 

    TnkAdPlugin::setEventHandler(this);


    return true;

}


void HelloWorld::showYourAd() {


    TnkAdPlugin::prepareInterstitialAd(TNK_PPI, 3);

}


// TnkAdEvnetHandler

void HelloWorld::onLoad(int handleId) {

    if (isOkToShow) {

        TnkAdPlugin::showInterstitialAd();

    }

}

 
[Interstitial Ad APIs]

API specs for prepareInterstitialAd() and showInterstitialAd().

- TnkPlugin::prepareInterstitialAd()

Method
  - void TnkPlugin::prepareInterstitialAd(char *displayLogic)
  - void TnkPlugin::prepareInterstitialAd(const char *displayLogic, int handleId)

Description
   It loads a single interstitial Ad from server and cache it in memory.

Parameters
 Name Description
 displayLogicEnter TNK_PPI for incentive ads or TNK_CPC for non-incentive ads.
Or other string for Display Logic. See 
5) Display Logic for details. 
 handleId When an event related to interstitial Ads occurs, callback is invoked with this value in the first parameter. 

- TnkPlugin::showInterstitialAd()

Method
  - void TnkPlugin::showInterstitialAd()

Description
   It displays an interstitial ad which is loaded by previous prepareInterstitialAd() invocation.

Comments