加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx-3.1 接入多盟广告sdk+Android (2)

发布时间:2020-12-14 14:18:24 所属栏目:百科 来源:网络整理
导读:上篇,把基本工作做完了,接下来,在C++下添加代码。 1、打开win32下的工程,添加一个类,DomobAd,代码如下, pre name="code" class="cpp"#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include jni.h#include "platform/android/jni/JniHelper.h"//#i

上篇,把基本工作做完了,接下来,在C++下添加代码。

1、打开win32下的工程,添加一个类,DomobAd,代码如下,


<pre name="code" class="cpp">#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
//#include <android/log.h>
#include "cocos2d.h"
#endif
using namespace cocos2d;
class DomobAd
{
public:
	DomobAd();
	virtual ~DomobAd();
	
	static void showInterstitial();
	/*static void initInterstitial();
	static void showBanner();
	static void hideBanner();*/
};

#include "DomobAd.h"
DomobAd::DomobAd(){}
DomobAd::~DomobAd(){}
//void DomobAd::showBanner()
//{
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
//	JniMethodInfo showBanner;
//	bool isHave = JniHelper::getStaticMethodInfo(showBanner,"org/cocos2dx/cpp/AppActivity","showBannerStatic","()V");
//	if (!isHave) {
//		CCLog("jni:showBannerStatic false");
//	}
//	else{
//
//		showBanner.env->CallStaticVoidMethod(showBanner.classID,showBanner.methodID);
//	}
//#endif
//}
//void DomobAd::hideBanner()
//{
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
//	JniMethodInfo hideBanner;
//	bool isHave = JniHelper::getStaticMethodInfo(hideBanner,"hideBannerStatic","()V");
//	if (!isHave) {
//		CCLog("jni:hideBannerStatic false");
//	}
//	else{
//		CCLog("jni:hideBannerStatic true");
//
//		hideBanner.env->CallStaticVoidMethod(hideBanner.classID,hideBanner.methodID);
//	}
//#endif
//}

void DomobAd::showInterstitial()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
	JniMethodInfo showInterstitial;
	bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,"showInterstitialStatic","()V");
	if (!isHave) {
		CCLog("jni:showInterstitial false");
	}
	else{
		CCLog("jni:showInterstitial true");

		showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID,showInterstitial.methodID);
	}
#endif
}

//void DomobAd::initInterstitial()
//{
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
//	JniMethodInfo initInterstitial;
//	bool isHave = JniHelper::getStaticMethodInfo(initInterstitial,"initInterstitialStatic","()V");
//	if (!isHave) {
//		CCLog("jni:initInterstitial false");
//	}
//	else{
//		CCLog("jni:initInterstitial true");
//
//		initInterstitial.env->CallStaticVoidMethod(initInterstitial.classID,initInterstitial.methodID);
//	}
//#endif
//}

#ifndef __APPMACROS_H__
#define __APPMACROS_H__

#include "cocos2d.h"



#define DESIGN_RESOLUTION_480X320    0
#define DESIGN_RESOLUTION_1024X768   1
#define DESIGN_RESOLUTION_2048X1536  2

/* If you want to switch design resolution,change next line */
#define TARGET_DESIGN_RESOLUTION_SIZE  DESIGN_RESOLUTION_480X320

typedef struct tagResource
{
    cocos2d::CCSize size;
    char directory[100];
}Resource;

static Resource smallResource  =  { cocos2d::CCSizeMake(480,320),"iphone" };
static Resource mediumResource =  { cocos2d::CCSizeMake(1024,768),"ipad"   };
static Resource largeResource  =  { cocos2d::CCSizeMake(2048,1536),"ipadhd" };

#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480,320);
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(1024,768);
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(2048,1536);
#else
#error unknown target design resolution!
#endif

// The font size 24 is designed for small resolution,so we should change it to fit for current design resolution
#define TITLE_FONT_SIZE  (cocos2d::CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width / smallResource.size.width * 24)

#endif /* __APPMACROS_H__ */
 
</pre><pre name="code" class="cpp">
//在helloworldscene.cpp添加如下代码即可:
<pre name="code" class="cpp"> CCMenuItemLabel  * closeInterstital=CCMenuItemLabel::create(CCLabelTTF::create("showInterstital","Helvetica",40),this,menu_selector(HelloWorld::menuShowInterstitialCallback));

void HelloWorld::menuShowInterstitialCallback(cocos2d::CCObject* pSender){
    DomobAd::showInterstitial();
}

//JniHelper 类是cocos提供的,具体用法:http://blog.csdn.net/luxiaoyu_sdc/article/details/15874505<a target=_blank href="http://blog.csdn.net/luxiaoyu_sdc/article/details/15874505">点击打开链接</a>
</pre><pre name="code" class="cpp" style="font-size: 13.3333339691162px;">下片再继续

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读