Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 游戏中嵌入广告(mogo横幅)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_944_2@转载请注明出处:http://www.voidcn.com/article/p-omllqquw-dy.html

@H_944_2@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.C@R_944_10636@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标签中

    <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类中,添加横幅广告

    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_673_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. :rgb(108,"onInterstitialStaleDated");
  27. returnfalse;
  28. }
  29. @Override
  30. voidonInterstitialRealClickAd(Stringarg0){
  31. //TODOAuto-generatedmethodstub
  32. Log.v("MogoCocos2dxDemo","onInterstitialRealClickAd");
  33. publicViewonInterstitialGetView(){
  34. :rgb(108,"onInterstitialGetView");
  35. returnbAnnerLayout;
  36. voidonInterstitialCloseAd(Booleanarg0){
  37. :rgb(108,"onInterstitialCloseAd");
  38. publicBooleanonInterstitialClickCloseButton(){
  39. :rgb(108,"onInterstitialClickCloseButton");
  40. voidonInterstitialClickAd(Stringarg0){
  41. :rgb(108,"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. voidon@R_874_10613@estAd(Stringarg0){
  57. :rgb(108,"on@R_874_10613@estAd");
  58. voidonReceiveAd(ViewGrouparg0,Stringarg1){
  59. :rgb(108,"onReceiveAd");
  60. voidonRealClickAd(){
  61. :rgb(108,"onRealClickAd");
  62. voidonFailedReceiveAd(){
  63. :rgb(108,"onFailedReceiveAd");
  64. voidonCloseMogoDialog(){
  65. :rgb(108,"onCloseMogoDialog");
  66. publicBooleanonCloseAd(){
  67. voidonClickAd(Stringarg0){
  68. :rgb(108,"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. @H_897_35@msg.what=0;
  104. handler.sendmessage(msg);
  105. //关闭横屏广告
  106. voidhideBAnnerStatic(){
  107. @H_897_35@msg.what=1;
  108. voidshowInterstitialStatic(){
  109. @H_897_35@msg.what=3;
  110. voidclose(){
  111. messagemsg=handler.obtainmessage();
  112. @H_897_35@msg.what=4;
  113. handler.sendmessage(msg);
  114. voidcloseInterstitial(){
  115. @H_897_35@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函数,显示广告

定义方法:

    #include"MOGOAd.h"
  1. @H_897_35@mOGOAd::MOGOAd(){}
  2. @H_897_35@mOGOAd::~MOGOAd(){}
  3. void@H_424_36@mOGOAd::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. void@H_424_36@mOGOAd::hideBAnner()
  14. {
  15. #if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
  16. JniMethodInfohideBAnner;
  17. @H_262_1196@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. void@H_424_36@mOGOAd::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. void@H_424_36@mOGOAd::close()
  30. :rgb(108,"org/cocos2dx/cpp/AdsMogoCoCos2dx","close",153); font-weight:bold; BACkground-color:inherit">void@H_424_36@mOGOAd::closeInterstitial()
  31. JniMethodInfocloseInterstitial;
  32. boolisHave=JniHelper::getStaticMethodInfo(closeInterstitial,"closeInterstitial",248)"> closeInterstitial.env->CallStaticVoidMethod(closeInterstitial.classID,closeInterstitial.methodID);

8、创建菜单按钮,点击显示广告
    CCSizewinSize=CCDirector::sharedDirector()->getWinSize();
  1. CCMenuItemLabel*bAnnerLable=CCMenuItemLabel::create(CCLabelTTF::create("showbAnner","Helvetica",30),:rgb(0,menu_SELEctor(HelloWorld::menuBAnnerCallBACk));
  2. bAnnerLable->setPosition(ccp(winSize.width-200,winSize.height-80));
    voidHelloWorld::menuCloseCallBACk(CCObject*pSender)
  1. @H_897_35@mOGOAd::close();
  2. voidHelloWorld::menuBAnnerCallBACk(CCObject*pSender){
  3. @H_897_35@mOGOAd::showBAnner();
  4. voidHelloWorld::menu@R_874_10613@esTinterstitialCallBACk(CCObject*pSender){
  5. @H_897_35@mOGOAd::hideBAnner();
  6. voidHelloWorld::menushowInterstitialCallBACk(CCObject*pSender){
  7. @H_897_35@mOGOAd::showInterstitial();
  8. voidHelloWorld::menucloseInterstitalCallBACk(CCObject*pSender){
  9. @H_897_35@mOGOAd::closeInterstitial();
  10. }

9、效果图

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x 游戏中嵌入广告(mogo横幅)全部内容,希望文章能够帮你解决Cocos2d-x 游戏中嵌入广告(mogo横幅)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:cocoscocos2ddx嵌入广告横幅游戏
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap