Cocos2d-x 游戏中嵌入广告(mogo横幅)

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 游戏中嵌入广告(mogo横幅)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载请注明出处:http://www.jb51.cc/article/p-omllqquw-dy.html

DEMO下载地址:http://download.csdn.net/detail/oyangyufu/7578451

在经过国内的几个广告平台尝试,最终选择芒果广告,有几个优点:SDK更新较快,支持cocos2d-x3.0,平台还聚合了国内主流广告平台,下载相应的SDK也可加载到工程中。

芒果横幅广告测试

1、将cocos2d-x项目导入到eclipse上

2、下载android SDK cocos2d-x 版本,网址:http://my.adsmogo.com/Apps/Download,下载完名称为:AdsMogo_Android_Cocos2d-x_Demo.zip

3、解压后将jar包放入libs目录下,单击右键->Build Path->Add to Build Path

4、在AndroidManifest.xml添加用户权限:

  1. <uses-permissionandroid:name="android.permission.INTERNET"/>
  2. <uses-permissionandroid:name="android.permission.MODIFY_AUdio_SETTINGS"/>
  3. <uses-permissionandroid:name="android.permission.INTERNET"/>
  4. <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
  5. <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
  6. <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
  7. <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
  8. <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
  9. <uses-permissionandroid:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
  10. <uses-permissionandroid:name="android.permission.CALL_PHONE"/>
  11. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  12. <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
  13. <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>
  14. <uses-permissionandroid:name="android.permission.VIBRATE"/>
  15. <uses-permissionandroid:name="android.permission.RECORD_AUdio"/>
  16. <uses-permissionandroid:name="android.permission.GET_TASKS"/>
  17. <uses-permissionandroid:name="android.permission.CAMERA"/>
  18. <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  19. <uses-permissionandroid:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>


5、声明广告平台Activity, 代码到AndroidMainfest.xml文件中的application标签

@H_404_193@
    <preclass="cpp"name="code"><activity
  1. android:name="com.adsmogo.adview.AdsMogoWebView"
  2. android:configChanges="orientation|keyboardHidden|screenSize"
  3. android:label="@string/app_name">
  4. </activity>
  5. <!--注册Service,自主广告与交换广告的应用下载功能所需-->
  6. <serviceandroid:name="com.adsmogo.controller.service.UpdateService"
  7. android:exported="true"
  8. android:process=":remote"/>
  9. <!--注册Service,安装应用所用-->
  10. <serviceandroid:name="com.adsmogo.controller.service.CountService"
  11. android:process=":remote"/>


6、在主Activity类中,添加横幅广告

@H_404_193@
    publicclassTestextendsCocos2dxActivity{
  1. privatestaticHandlerhandler;
  2. staticRelativeLayoutbannerLayout;
  3. privateAdsMogoLayoutadView;
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. bannerLayout=newRelativeLayout(this);
  7. RelativeLayout.LayoutParamsparentLayputParams=newRelativeLayout.LayoutParams(
  8. RelativeLayout.LayoutParams.FILL_PARENT,
  9. RelativeLayout.LayoutParams.FILL_PARENT);
  10. this.addContentView(bannerLayout,parentLayputParams);
  11. /**
  12. @H_509_404@*初始化全插屏对象
  13. *初始化之前必须设置默认的AppKey和Activity
  14. */
  15. AdsMogoInterstitialManager.setDefaultInitAppKey("93535c6092f543e8a257ee435a69da06");
  16. AdsMogoInterstitialManager.setInitActivity(Test. AdsMogoInterstitialManager.shareInstance()
  17. .initDefaultInterstitial();
  18. .defaultInterstitial()
  19. .setAdsMogoInterstitialListener(newAdsMogoInterstitialListener(){
  20. @Override
  21. voidonShowInterstitialScreen(Stringarg0){
  22. //TODOAuto-generatedmethodstub
  23. Log.v("MogoCocos2dxDemo","onShowInterstitialScreen");
  24. }
  25. publicbooleanonInterstitialStaleDated(Stringarg0){
  26. "onInterstitialStaleDated");
  27. returnfalse;
  28. }
  29. @Override
  30. voidonInterstitialRealClickAd(Stringarg0){
  31. //TODOAuto-generatedmethodstub
  32. Log.v("MogoCocos2dxDemo","onInterstitialRealClickAd");
  33. publicViewonInterstitialGetView(){
  34. "onInterstitialGetView");
  35. returnbannerLayout;
  36. voidonInterstitialCloseAd(booleanarg0){
  37. "onInterstitialCloseAd");
  38. publicbooleanonInterstitialClickCloseButton(){
  39. "onInterstitialClickCloseButton");
  40. voidonInterstitialClickAd(Stringarg0){
  41. "onInterstitialClickAd");
  42. publicClassgetCustomEvemtPlatformAdapterClass(
  43. AdsMogoCustomEventPlatformEnumarg0){
  44. returnnull;
  45. });
  46. //
  47. handler=newHandler(){
  48. voidhandleMessage(Messagemsg){
  49. switch(msg.what){
  50. case0:
  51. //展示Banner
  52. if(bannerLayout.getChildCount()==0){
  53. adView=newAdsMogoLayout(Test.this,
  54. "46184ad32a0c417887cb433203b11403");
  55. adView.setAdsMogoListener(newAdsMogoListener(){
  56. voidonRequestAd(Stringarg0){
  57. "onRequestAd");
  58. voidonReceiveAd(ViewGrouparg0,Stringarg1){
  59. "onReceiveAd");
  60. voidonRealClickAd(){
  61. "onRealClickAd");
  62. voidonFailedReceiveAd(){
  63. "onFailedReceiveAd");
  64. voidonCloseMogoDialog(){
  65. "onCloseMogoDialog");
  66. publicbooleanonCloseAd(){
  67. voidonClickAd(Stringarg0){
  68. "onClickAd");
  69. RelativeLayout.LayoutParamslayoutParams= RelativeLayout.LayoutParams.WRAP_CONTENT);
  70. layoutParams.addRule(
  71. RelativeLayout.ALIGN_PARENT_BOTTOM,
  72. RelativeLayout.TRUE);
  73. bannerLayout.addView(adView,layoutParams);
  74. break;
  75. case1:
  76. onClickHideShow();
  77. break;
  78. case3:
  79. /**
  80. *进入展示时机
  81. *当应用需要展示全屏广告调用interstitialShow(booleanisWait);
  82. *通知SDK进入展示时机,SDK会竭尽全力展示出广告,当然由于网络等问题不能立即展示
  83. *广告的,您可以通过参数isWait来控制授权SDK在获得到广告后立即展示广告。
  84. */
  85. AdsMogoInterstitialManager.shareInstance()
  86. .defaultInterstitial().interstitialShow(true);
  87. case4:
  88. Test.this.finish();
  89. case5:
  90. *退出展示时机
  91. *如果您之前进入了展示时机,并且isWait参数设置为YES,那么在需要取消等待广告展示的
  92. *时候调用方法interstitialCancel();来通知SDK
  93. .defaultInterstitial().interstitialCancel();
  94. };
  95. publicCocos2dxGLSurfaceViewonCreateView(){
  96. Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
  97. //Testshouldcreatestencilbuffer
  98. glSurfaceView.setEGLConfigChooser(5,6,5,16,8);
  99. returnglSurfaceView;
  100. //显示横屏广告
  101. staticvoidshowBannerStatic(){
  102. Messagemsg=handler.obtainMessage();
  103. msg.what=0;
  104. handler.sendMessage(msg);
  105. //关闭横屏广告
  106. voidhideBannerStatic(){
  107. msg.what=1;
  108. voidshowInterstitialStatic(){
  109. msg.what=3;
  110. voidclose(){
  111. Messagemsg=handler.obtainMessage();
  112. msg.what=4;
  113. handler.sendMessage(msg);
  114. voidcloseInterstitial(){
  115. msg.what=5;
  116. static{
  117. System.loadLibrary("cocos2dcpp");
  118. voidonClickHideShow(){
  119. if(adView!=null){
  120. adView.setVisibility(adView.getVisibility()==View.VISIBLE?View.GONE
  121. :View.VISIBLE);
  122. }



7、CCP程序中jni调用java函数显示广告

定义方法

@H_404_193@
    #include"MOGOAd.h"
  1. MOGOAd::MOGOAd(){}
  2. MOGOAd::~MOGOAd(){}
  3. voidMOGOAd::showBanner()
  4. {
  5. #if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
  6. JniMethodInfoshowBanner;
  7. boolisHave=JniHelper::getStaticMethodInfo(showBanner,"com/cocos2dx/org/Test","showBannerStatic","()V");
  8. if(!isHave){
  9. CCLog("jni:showBannerStaticfalse");
  10. }else{
  11. showBanner.env->CallStaticVoidMethod(showBanner.classID,showBanner.methodID);
  12. #endif
  13. voidMOGOAd::hideBanner()
  14. {
  15. #if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
  16. JniMethodInfohideBanner;
  17. boolisHave=JniHelper::getStaticMethodInfo(hideBanner,"hideBannerStatic","()V");
  18. if(!isHave){
  19. CCLog("jni:hideBannerStaticfalse");
  20. }else{
  21. CCLog("jni:hideBannerStatictrue");
  22. hideBanner.env->CallStaticVoidMethod(hideBanner.classID,hideBanner.methodID);
  23. voidMOGOAd::showInterstitial()
  24. JniMethodInfoshowInterstitial;
  25. boolisHave=JniHelper::getStaticMethodInfo(showInterstitial,"showInterstitialStatic",248)"> CCLog("jni:hideBannerStaticfalse");
  26. CCLog("jni:hideBannerStatictrue");
  27. showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID,showInterstitial.methodID);
  28. #endif
  29. voidMOGOAd::close()
  30. "org/cocos2dx/cpp/AdsMogoCoCos2dx","close",153); font-weight:bold; background-color:inherit">voidMOGOAd::closeInterstitial()
  31. JniMethodInfocloseInterstitial;
  32. boolisHave=JniHelper::getStaticMethodInfo(closeInterstitial,"closeInterstitial",248)"> closeInterstitial.env->CallStaticVoidMethod(closeInterstitial.classID,closeInterstitial.methodID);

8、创建菜单按钮,点击显示广告
@H_404_193@
    CCSizewinSize=CCDirector::sharedDirector()->getWinSize();
  1. CCMenuItemLabel*bannerLable=CCMenuItemLabel::create(CCLabelTTF::create("showbanner","Helvetica",30), bannerLable->setPosition(ccp(winSize.width-200,winSize.height-80));
@H_404_193@
    voidHelloWorld::menuCloseCallback(CCObject*pSender)
  1. MOGOAd::close();
  2. voidHelloWorld::menuBannerCallback(CCObject*pSender){
  3. MOGOAd::showBanner();
  4. voidHelloWorld::menuRequestInterstitialCallback(CCObject*pSender){
  5. MOGOAd::hideBanner();
  6. voidHelloWorld::menushowInterstitialCallback(CCObject*pSender){
  7. MOGOAd::showInterstitial();
  8. voidHelloWorld::menucloseInterstitalCallback(CCObject*pSender){
  9. MOGOAd::closeInterstitial();
  10. }

9、效果

原文链接:https://www.f2er.com/cocos2dx/341670.html

猜你在找的Cocos2d-x相关文章