Adding Interstitial Ad to your Cocos-2dx app

The Audience Network C++ SDK allows you to monetize your Cocos2d-x apps with Facebook ads. An Interstitial ad is a full screen ad that you can show in your app.


Let's implement the following Interstitial ad placement.


Load and Show Interstitial Ad

Follow the Audience Network Getting Started guide and ensure you have created a Facebook app and an Audience Network placement.


Follow the Audience Network C++ SDK Getting Started guides and ensure you have successfully configured the Cocos2d-x platform and have one Cocos2d-x sample app created.

  1. Include InterstitialAd.h file in your Cocos2d-x sample app as following:
    #include "InterstitialAd.h"
    using namespace facebook::audience_network;
    ...
    //create an interstitialAd instance using std::shared_ptr
    std::shared_ptr<facebook::audience_network::InterstitialAd> interstitialAd = 
      std::make_shared<InterstitialAd>("YOUR_PLACEMENT_ID");
    Replace YOUR_PLACEMENT_ID with your own placement id string. If you don't have a placement id or don't know how to get one, refer to the Audience Network Getting Started guide.

  2. Your app needs to be set with various callbacks from InterstitialAd instance.
    For example, your app needs to call the show() function after the ad is loaded. In another case, if the ad is fails to load, your app needs to log the error message. The callbacks from InterstitialAd are called as following:
    void adDidLoad(std::shared_ptr<InterstitialAd> ad) 
    {
      // Ad is loaded.
    }
    
    void adWillLogImpression(std::shared_ptr<InterstitialAd> ad) 
    {
      // Ad has an impression from the user.
    }
    
    void adDidClick(std::shared_ptr<InterstitialAd> ad) 
    {
      // Ad is clicked by the user.
    }
    
    void adWillClose(std::shared_ptr<InterstitialAd> ad) 
    {
      // User will close the ad.
    }
    
    void adDidClose(std::shared_ptr<InterstitialAd> ad) 
    {
      // User has closed the ad.
    }
    
    void adDidFailWithError(std::shared_ptr<InterstitialAd> ad, std::string error) 
    {
      // Ad is failed to load with error message.
    }
    
    interstitialAd->setDidLoad(adDidLoad);
    interstitialAd->setWillLogImpression(adWillLogImpression);
    interstitialAd->setDidClick(adDidClick);
    interstitialAd->setWillClose(adWillClose);
    interstitialAd->setDidClose(adDidClose);
    interstitialAd->setDidFailWithError(adDidFailWithError);
  3. After the callbacks are set, you can call the loadAd() method to load an Interstitial ad.
    When the ad is loaded, you can call show() to present an Interstitial ad in your app.