上一篇已经完成特性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中添加:

        <activity android:name="com.google.ads.AdActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在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这个小游戏的开发已经全部完成了。

cocos2d-x手游性能优化总结

近段时间在使用cocos2d-x开发2D手游,技术方案使用的是cocos2d-x+lua,因为游戏使用的是cocos2d-x 2.1.5版本,有些优化方案在最新版的cocos2d-x版本已经实现...

阅读全文

cocos2dx-html5 实现网页版flappy bird游戏

我也是第一次使用cocos2d_html5,对js和html5也不熟,看引擎自带的例子和引擎源码,边学边做,如果使用过cocos2d-x的话,完成这个游戏还是十分简单的。游戏体...

阅读全文

【cocos2d-x开发实战 特训99-part6】完成游戏首页

现在特性99游戏的功能基本完成了,但是游戏没有首页也挺奇怪,所以这篇博客为游戏添加首页,通过首页进入游戏。 首页的元素比较简单,就是一张背景图,一个l...

阅读全文

23 条评论

  1. 给楼主分享的文章点个赞。楼主你不妨试试keymob平台。keymob平台也有自己的广告形式。我自己的应用现在用的就是keymob平台的广告,发现展现量与收入挺不错的。我把一个好东西分享给楼主。希望能够对楼主起到帮助哦。www.keymob.com上面有Keymob集成广告的教程与文档可以去看看的。

    1. Joost “De klantenservice, die per telefoon bereikbaar is.”, deze zin kun je bijna wel weg laten. Het duurde zeker een half uur voordat ik iemand aan de lijn kreeg bij Vodafone. Al met al liepen de kosten hierdoor flink op! Ik steun je punt, dat ze vaak ook niet precies weten, wat het antwoord is op je vraag, wat toch wel een verplichting is, na die lange wachttijd en hoge kosten!In de winkel ben ik inderdaad ook goed geholpen, daar blinken ze bij Vodafone dan wel weer in uit!

  2. 老师, 您好,请问SuperManActivity这部分代码添加到哪里?您的源码中没找到

  3. @Alex Zhou 但是 我利用你的代码生成的APK在同一台手机尺寸就和电脑上的 几乎一致 还有一个问题 就是最后admob的广告会把 所有用户界面挡住 我的QQ 是961438311 你能帮我看下吗? 万分感谢 到了最后 不弄明白 心里也不舒服 嘿嘿 才学比较笨

  4. 你好 请问 为什么 我照着你的代码敲出来运行的android端程序画面失真 和在PC上面看到的不一样

  5. 博主,想问一下,为什么android的编译不通过。报这个错:
    The Selected NDK toolchain version was 4.8 !
    Android NDK: WARNING:/Users/yingxian/workspace/SuperMan/proj.android/../cocos2d/cocos/2d/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries
    Android NDK: WARNING:/Users/yingxian/workspace/SuperMan/proj.android/../cocos2d/cocos/2d/platform/android/Android.mk:cocos2dxandroid_static: LOCAL_LDLIBS is always ignored for static libraries
    make: Entering directory `/Users/yingxian/workspace/SuperMan/proj.android’
    [armeabi] Compile++ thumb: cocos2dcpp_shared <= main.cpp
    [armeabi] Compile++ thumb: cocos2dcpp_shared <= AppDelegate.cpp
    [armeabi] Compile++ thumb: cocos2dcpp_shared <= GLES-Render.cpp
    [armeabi] Compile++ thumb: cocos2dcpp_shared <= GameScene.cpp
    FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
    FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
    make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/hellocpp/main.o] Error 1
    make: *** Waiting for unfinished jobs….
    make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/AppDelegate.o] Error 1
    FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
    make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/GLES-Render.o] Error 1
    FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
    make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/GameScene.o] Error 1
    make: Leaving directory `/Users/yingxian/workspace/SuperMan/proj.android'
    Traceback (most recent call last):
    File "./build_native.py", line 166, in
    build(opts.ndk_build_param,opts.android_platform,opts.build_mode)
    File “./build_native.py”, line 153, in build
    do_build(cocos_root, ndk_root, app_android_root,ndk_build_param,sdk_root,android_platform,build_mode)
    File “./build_native.py”, line 89, in do_build
    raise Exception(“Build dynamic library for project [ ” + app_android_root + ” ] fails!”)
    Exception: Build dynamic library for project [ /Users/yingxian/workspace/SuperMan/proj.android ] fails!

    1. 解决了吗 ?我也碰到同样的问题 ,需要解决。解决了告诉一下,

  6. 这个没放上去的,csdn上那个代码是最新的,我这跑起来正常呢,我记得是在runOnUIThread里弹出广告PopupWindow的,应该没啥问题啊。

欢迎留言