Insert AD (banners) in Cocos2d-x games)

Source: Internet
Author: User

Insert AD (banners) in Cocos2d-x games)

After several domestic advertising platform attempts, the final choice of Mango advertising, there are several advantages: SDK updates faster, support cocos2d-x3.0, the platform also aggregates the mainstream domestic advertising platform, you can also download the corresponding SDK to the project.

Mango banner advertising test

1. Import cocos2d-x project to eclipse

2. Download the android SDK cocos2d-x version.

3. decompress the package and place it in the libs directory. Right-click Build Path and choose Add to Build Path.

4. Add User Permissions in AndroidManifest. xml:

 
  
   
    
     
      
       
        
         
          
           
            
             
              
               
                
                 
                   
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 


5. Declare the Activity of the advertising platform and code it to the application tag in the AndroidMainfest. xml file.

 
  
 
  
  
  
 

 

6. Add a banner advertisement to the main Activity class.

 

Public class Test extends Cocos2dxActivity {private static Handler handler; private static RelativeLayout bannerLayout; private AdsMogoLayout adView; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); bannerLayout = new RelativeLayout (this); RelativeLayout. layoutParams parentLayputParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. FILL_PARENT, Relativ ELayout. layoutParams. FILL_PARENT); this. addContentView (bannerLayout, parentLayputParams);/*** the default AppKey and Activity */AdsMogoInterstitialManager must be set before the full-screen object * initialization. setDefaultInitAppKey (93535c6092f543e8a257ee435a69da06); AdsMogoInterstitialManager. setInitActivity (Test. this); AdsMogoInterstitialManager. your instance (). initdefainterinterstitial (); AdsMogoInterstitialManager. your instance (). defaultInterstiti Al (). setAdsMogoInterstitialListener (new AdsMogoInterstitialListener () {@ Overridepublic void onShowInterstitialScreen (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onShowInterstitialScreen) ;}@ Overridepublic boolean onInterstitialStaleDated (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInterstitialStaleDated); return false ;}@ Overridepublic void OnInterstitialRealClickAd (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInterstitialRealClickAd) ;}@ Overridepublic View onInterstitialGetView () {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInterstitialGetView); return bannerLayout;} @ Overridepublic void onInterstitialCloseAd (boolean arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInter StitialCloseAd) ;}@ Overridepublic boolean onInterstitialClickCloseButton () {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInterstitialClickCloseButton); return false ;}@ Overridepublic void onInterstitialClickAd (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onInterstitialClickAd) ;}@ Overridepublic Class getCustomEvemtPlatformAdapterClass (AdsMogoCustomEventPlatf OrmEnum arg0) {// TODO Auto-generated method stubreturn null ;}}); // handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case 0: // display Bannerif (bannerLayout. getChildCount () = 0) {adView = new AdsMogoLayout (Test. this, 46184ad32a0c0000887cb433203b11403); adView. setAdsMogoListener (new AdsMogoListener () {@ Overridepublic void onRequ EstAd (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onRequestAd) ;}@ Overridepublic void onReceiveAd (ViewGroup arg0, String arg1) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onReceiveAd) ;}@ Overridepublic void onRealClickAd () {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onRealClickAd) ;}@ Overridepublic void onFailedReceiveAd () {// TODO Auto-gen Erated method stubLog. v (MogoCocos2dx Demo, onFailedReceiveAd) ;}@ Overridepublic void onCloseMogoDialog () {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onCloseMogoDialog);} @ Overridepublic boolean onCloseAd () {// TODO Auto-generated method stubreturn false;} @ Overridepublic void onClickAd (String arg0) {// TODO Auto-generated method stubLog. v (MogoCocos2dx Demo, onClickAd) ;}@ Overridepublic Class getCustomEvemtPlatformAdapterClass (AdsMogoCustomEventPlatformEnum arg0) {// TODO Auto-generated method stubreturn null ;}}); RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. FILL_PARENT, RelativeLayout. layoutParams. WRAP_CONTENT); layoutParams. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM, RelativeLayout. TRUE); bannerLayout. addView (adView, layoutP Arams);} break; case 1: onClickHideShow (); break; case 3:/*** enter the display time * Call interstitialShow (boolean isWait) when the application needs to display full-screen ads ); * When the SDK is notified to enter the display time, the SDK will do its utmost to display the advertisement. Of course, the SDK cannot immediately display the * advertisement due to network problems, you can use isWait to control the authorization SDK to display ads immediately after the ads are obtained. */AdsMogoInterstitialManager. your instance (). defaultInterstitial (). interstitialShow (true); break; case 4: Test. this. finish (); break; case 5:/*** exit display time * If you have entered the display time before and set the isWait parameter to YES, call interstitialCancel () to notify SDK */AdsMogoInterstitialManager. your instance (). defaultInterstitial (). interstitialCancel (); break ;}};} public Cocos2dxGLSurfaceView onCreateView () {Cocos2dxGLSurfaceView glS UrfaceView = new Cocos2dxGLSurfaceView (this); // Test shocould create stencel buffer glSurfaceView. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} // display the public static void showBannerStatic () {Message msg = handler. obtainMessage (); msg. what = 0; handler. sendMessage (msg);} // disable the public static void hideBannerStatic () {Message msg = handler. obtainMessage (); msg. what = 1; handler. sendMessa Ge (msg);} public static void showInterstitialStatic () {Message msg = handler. obtainMessage (); msg. what = 3; handler. sendMessage (msg);} public static void close () {Message msg = handler. obtainMessage (); msg. what = 4; handler. sendMessage (msg);} public static void closeInterstitial () {Message msg = handler. obtainMessage (); msg. what = 5; handler. sendMessage (msg);} static {System. loadLibrary (cocos2dcpp);} pub Lic void onClickHideShow () {if (adView! = Null) {adView. setVisibility (adView. getVisibility () = View. VISIBLE? View. GONE: View. VISIBLE );}}}


 

 


7. The jni in the CCP program calls the java function to display the advertisement

Definition method:

 

#include MOGOAd.hMOGOAd::MOGOAd(){}MOGOAd::~MOGOAd(){}void MOGOAd::showBanner() {#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  JniMethodInfo showBanner;bool isHave = JniHelper::getStaticMethodInfo(showBanner,com/cocos2dx/org/Test,showBannerStatic,()V);   if (!isHave) { CCLog(jni:showBannerStatic false);}else{showBanner.env->CallStaticVoidMethod(showBanner.classID, showBanner.methodID);}  #endif} void MOGOAd::hideBanner(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  JniMethodInfo hideBanner;bool isHave = JniHelper::getStaticMethodInfo(hideBanner,com/cocos2dx/org/Test,hideBannerStatic,()V); if (!isHave) { CCLog(jni:hideBannerStatic false);}else{CCLog(jni:hideBannerStatic true);hideBanner.env->CallStaticVoidMethod(hideBanner.classID, hideBanner.methodID); }  #endif}void MOGOAd::showInterstitial(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  JniMethodInfo showInterstitial;bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,com/cocos2dx/org/Test,showInterstitialStatic,()V); if (!isHave) { CCLog(jni:hideBannerStatic false);}else{CCLog(jni:hideBannerStatic true);showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID, showInterstitial.methodID); }  #endif}void MOGOAd::close(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  JniMethodInfo showInterstitial;bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,org/cocos2dx/cpp/AdsMogoCoCos2dx,close,()V); if (!isHave) { CCLog(jni:hideBannerStatic false);}else{CCLog(jni:hideBannerStatic true);showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID, showInterstitial.methodID); }  #endif}  void MOGOAd::closeInterstitial(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  JniMethodInfo closeInterstitial;bool isHave = JniHelper::getStaticMethodInfo(closeInterstitial,org/cocos2dx/cpp/AdsMogoCoCos2dx,closeInterstitial,()V); if (!isHave) { CCLog(jni:hideBannerStatic false);}else{CCLog(jni:hideBannerStatic true);closeInterstitial.env->CallStaticVoidMethod(closeInterstitial.classID, closeInterstitial.methodID); }  #endif} 

8. Create a menu button and Click Show Advertisement
CCSize winSize = CCDirector::sharedDirector()->getWinSize();CCMenuItemLabel  * bannerLable=CCMenuItemLabel::create(CCLabelTTF::create(show banner, Helvetica, 30),this, menu_selector(HelloWorld::menuBannerCallback));bannerLable->setPosition(ccp(winSize.width -200, winSize.height -80));
void HelloWorld::menuCloseCallback(CCObject *pSender){MOGOAd::close();}void HelloWorld::menuBannerCallback(CCObject *pSender){MOGOAd::showBanner();}void HelloWorld::menuRequestInterstitialCallback(CCObject *pSender){MOGOAd::hideBanner();}void HelloWorld::menushowInterstitialCallback(CCObject *pSender){MOGOAd::showInterstitial();}void HelloWorld::menucloseInterstitalCallback(CCObject *pSender){MOGOAd::closeInterstitial();}

9,

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.