月度存档: 12 月 2016

cocos2dx 3.x Android中加入AdMob

观察 AppActivity.java:
public class AppActivity extends Cocos2dxActivity
启动的Activity是继承Cocos2dxActivity;

Cocos2dxActivity 存在 mFrameLayout,那就直接在里面设置个PopupWindow即可;

Activity本身空无一物,直接加上一下代码即可:

public class AppActivity extends Cocos2dxActivity {
    private AdView adView;
    private PopupWindow popUp;
    private static AppActivity _activity;
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _activity = this;
        adView = new AdView(this); 
        adView.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"); // 这里填自己的AdUnitId
        adView.setAdSize(AdSize.BANNER);
        adView.setAdListener(new AdmobListener()); // AdmobListener 中处理收到事件
    }
    public static void showHideAdPopup() { // 静态方法供Native代码调用
        _activity._showHideAdPopup();
    }
    public void _showHideAdPopup() {
        if (popUp == null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        popUp = new PopupWindow(_activity);
                        popUp.setWidth(360);
                        popUp.setHeight(50);
                        popUp.setWindowLayoutMode(WindowManager.LayoutParams.WRAP_CONTENT,
                                WindowManager.LayoutParams.WRAP_CONTENT);
                        popUp.setClippingEnabled(false);
                        layout = new LinearLayout(_activity);
                        layout.setPadding(-10, -10, -10, -10); // 不加这句AdView可能会没有足够Size容纳加载不了
                        MarginLayoutParams params = new MarginLayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.MATCH_PARENT);
                        params.setMargins(0, 0, 0, 0);
                        layout.setOrientation(LinearLayout.VERTICAL);
                        layout.addView(adView, params);
                        popUp.setContentView(layout);

                        AdRequest adRequest = new AdRequest.Builder()
                                .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxx") // 申明测试设备,不加乱点会被ban账号,在logcat中搜AdRequest可以看到
                                .build();
                        _activity.adView.loadAd(adRequest);

                        popUp.showAtLocation(mFrameLayout, Gravity.BOTTOM, 0, 0);
                        popUp.update();
                    } catch (Exception ex) {
                        Toast.makeText(AppActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else { // 再次调用隐藏
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    layout.removeView(adView);
                    popUp.dismiss();
                    popUp = null;
                }
            });
        }
    }
    private class AdmobListener extends AdListener {
        @Override
        public void onAdClosed() {
            Log.d("", "Closed");
            super.onAdClosed();
        }
        @Override
        public void onAdFailedToLoad(int errorCode) {
            Log.d("", "Failed");
            super.onAdFailedToLoad(errorCode);
        }
        @Override
        public void onAdLeftApplication() {
            Log.d("", "Left");
            super.onAdLeftApplication();
        }
        @Override
        public void onAdLoaded() {
            Log.d("", "Loaded");
            super.onAdLoaded();
        }
        @Override
        public void onAdOpened() {
            Log.d("", "Opened");
            super.onAdOpened();
        }
    }
    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
}

Native 代码中通过cocos2d::JniHelper交互
记得判断CC_TARGET_PLATFORM,防止XCode编译ios时出错

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
const char* NativeActivityClassName = "org/cocos2dx/cpp/AppActivity";
#endif

void AdmobHelper::showAds() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t
                                                , NativeActivityClassName
                                                , "showHideAdPopup"
                                                , "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
#endif
}

cocos2dx 单点点击事件

1、通过cocos2d::EventListenerTouchOneByOne::create()创建 listener;
2、多个listener addEventListenerWithSceneGraphPriority 按z-order顺序触发,需要自行判断点击范围;
3、onTouchBegan 中返回值为true才会触发后续Moved Ended Cancelled事件;
4、setSwallowTouches(true) 且onTouchBegan返回true时,不会触发其他的listener;