欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

【cocos2d-x开发实战 特训99-终结篇】移植到android平台和添加admob广告 转

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:火龙论坛正试运营阶段,欢迎大家多提些建设性意见……
摘自:http://codingnow.cn/cocos2d-x/1459.html

上一篇已经完成特性99在win32平台下的开发,现在把它移植到android上,首先修改Android.mk文件,内容如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
# 遍历目录及子目录的函数
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
# 遍历Classes目录
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
FILE_LIST := hellocpp/main.cpp
# 从所有文件中提取出所有.cpp文件
FILE_LIST += $(filter %.cpp, $(ALLFILES))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,2d)
$(call import-module,audio/android)
$(call import-module,Box2D)
$(call import-module,extensions)

修改GameScene.cpp文件:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "extensions/cocos-ext.h"
#else
#include "cocos-ext.h"
#endif

修改AndroidManifest.xml,指定为竖屏:

android:screenOrientation="portrait"

切换到项目的proj.android目录下,执行:

python build_native.py

编译完成后,运行项目到手机,效果如图:


接下来给项目添加admob广告,首先得去admob广告注册一个账号,下载GoogleAdMobAdsSdk-6.4.1.jar文件,复制到项目的libs目录下。
因为cocos2d-x 3.0使用NativeActivity,所以这里自定义一个SuperManActivity,继承自NativeActivity,内容如下:

public class SuperManActivity extends NativeActivity {

static AdView adView;
static SuperManActivity _activity;
static PopupWindow popUp;
static LinearLayout mainLayout;
static boolean adsinited = false;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_activity = this;
adView = new AdView(_activity, AdSize.BANNER, "你的id");
}

private static void showAdPopup() {
Log.d("supermanactivity", "adsinited=" + adsinited);
if (adsinited) {
return;
}

if (adView != null) {
_activity.runOnUiThread(new Runnable() {

@Override
public void run() {
Log.d("supermanactivity ", "runOnUiThread");
adsinited = true;

// Out popup window
popUp = new PopupWindow(_activity);

// This is the minimum size for AdMob, we need to set this

// in case our target device run at 320x480 resolution

// (Otherwise no ad will be shown, see the padding kill

// below)

int screenWidth = _activity.getWindowManager()
.getDefaultDisplay().getWidth();
popUp.setWidth(screenWidth);
popUp.setHeight(50);
popUp.setWindowLayoutMode(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popUp.setClippingEnabled(false);
LinearLayout layout = new LinearLayout(_activity);
mainLayout = new LinearLayout(_activity);

// The layout system for the PopupWindow will kill some

// pixels due to margins/paddings etc… (No way to remove

// it), so padd it to adjust
layout.setPadding(0, 0, 0, 0);
MarginLayoutParams params = new MarginLayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
params.setMargins(0, 0, 0, 0);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView, params);
popUp.setContentView(layout);
_activity.setContentView(mainLayout, params);
AdRequest adRequest = new AdRequest();

// Enable this if your are testing AdMob, otherwise you'll

// risk to be banned!

// adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
_activity.adView.loadAd(adRequest);

// Show our popup window
popUp.showAtLocation(mainLayout, Gravity.TOP, 0, 0);
popUp.update();
}
});
}
}

// Do some cleanup
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}

public static void changeAdShow(final boolean show) {
Log.d("SuperManActivity", "show=" + show + "; adsinited=" + adsinited);
if (!adsinited) {
showAdPopup();
} else {
_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!show) {
popUp.dismiss();
} else {
popUp.showAtLocation(mainLayout, Gravity.TOP, 0, 0);
popUp.update();
}
}
});
}
}
}

在AndroidManifest.xml中添加:


在main.cpp中添加showAds函数:

void showAds(bool show)
{
JniMethodInfo t;
if(JniHelper::getStaticMethodInfo(t, "com/alexzhou/superman/SuperManActivity", "changeAdShow", "(Z)V"))
{
LOGD("showAds show");
jboolean jshow = show;
t.env->CallStaticVoidMethod(t.classID, t.methodID, jshow);
}
}

广告显示的区域是在顶部,而且只在首页和游戏结束时显示,所以在WelcomeScene.cpp中添加:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
extern void showAds(bool show);
#endif

然后再WelcomeScene::init函数最后添加:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
showAds(true);
#endif

在GameScene.cpp中添加:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
extern void showAds(bool show);
#endif

然后在GameScene::explosionEndDid函数最后添加:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
showAds(true);
#endif

在GameScene::init函数最后添加:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
showAds(false);
#endif

重新编译,运行项目,效果如图:

   

 

 

OK,到此为止,特性99这个小游戏的开发已经全部完成了。
游戏源码下载地址:http://download.csdn.net/detail/zhoujianghai/7047139

发表评论