前言:

这一节,笔者将给flappy加入google的AdMob广告 ,这也是为什么越南小伙瞬间日进5万美元的秘诀;平台还是android手机上,通过这个demo,读者可以学习js代码怎么和android代码交互,怎么按键检测退出,怎么分享;

ps 如果没有出现广告,确定wifi是否打开,确定杀毒安全软件是否禁止广告模块;

背景:

最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山寨了一个一模一样的游戏,现在把游戏的思路和源码分享出来,代码是基于javascript语言,cocos2d-x游戏引擎,cocos2d-x editor手游开发工具完成的,请读者轻砸;

ps:运行demo需要配置好,暂不支持其他工具。还有demo是跨平台的,可移植运行android,ios,html5网页等。

Android源代码:

进入代码集中营下载(flappy源码–添加admob广告):  

效果图 (图片顶部有广告)

SouthEastSouthEast

代码分析

首先实现广告模块:在以前android代码的基础上加入GoogleAdmobAdsSdk.jar,拷贝到libs路径下,并且右击选择add as library;

然后在src目录下加入plugin模块。

SouthEast

接着修改asserts里面的MainActivity.js,在onEnter方法里面加入广告代码;a152fcf037adb43是广告秘钥,可到admob官网申请,showAds表示广告的位置;

var adsAdmob = plugin.PluginManager.getInstance().loadPlugin("AdsAdmob");    cc.log("configDeveloperInfo AdsAdmob");    if (adsAdmob) {        cc.log("configDeveloperInfo");        adsAdmob.configDeveloperInfo({"AdmobID": "a152fcf037adb43"});        adsAdmob.showAds(0, 0, 1);    }    else {        cc.log("can't find AdsAdmob plugin");    }

找到Cocos2dMainActivity文件,整个游戏场景作为一个activity存在,修改代码如下;初始化了PluginWarpper,同时可以添加返回按键事件onBackPressed();System.loadLibrary(“cocos2dx-game”)也很关键,把需要的库下载下来;在AndroidManifest修改相应的参数,代码不列出来;运行可以看到广告,也可以点击返回键弹出退出框;

public class Cocos2dMainActivity extends Cocos2dxActivity {    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        PluginWrapper.init(this); // for plugins        PluginWrapper.setGLSurfaceView(Cocos2dxGLSurfaceView.getInstance());        Cocos2dxLocalStorage.context = this;        /*String languageType = getResources().getConfiguration().locale.getCountry();        if (languageType.equals("CN")) {            Cocos2dxLocalStorage.setItem("language", "CN");            System.out.println("language china");        } else {            Cocos2dxLocalStorage.setItem("language", "US");            System.out.println("language english");        }*/    }    public Cocos2dxGLSurfaceView onCreateView() {        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);        // HelloAds should create stencil buffer        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);        return glSurfaceView;    }    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            onBackPressed();            return true;        }        return super.dispatchKeyEvent(event);    }    public void onBackPressed() {        AlertDialog.Builder builder = new AlertDialog.Builder(this)                .setTitle("")                .setMessage("quit the game?")                .setNegativeButton("Yes", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                        System.exit(0);                        finish();                    }                }).setPositiveButton("No", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                    }                });        AlertDialog dialog = builder.create();        dialog.show();    }    public void onClickShare(View view) {        Intent intent = new Intent(Intent.ACTION_SEND);        intent.setType("p_w_picpath/*");        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");        intent.putExtra(Intent.EXTRA_TEXT, "终于可以了!!!");        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        startActivity(Intent.createChooser(intent, getTitle()));    }    static {//        System.loadLibrary("game");        System.loadLibrary("cocos2dx-game");    }}

接着我们实现在游戏里面触发分享事件,找到asserts–MainLayer.js, 给那个123图片添加点击事件;sys.localStorage.setItem(“share”, “xxx”)是交流通道,获得关键词share,直接在CocosdLocalStorge setItem方法下触发;

MainLayer.prototype.onGradeClicked = function () {    sys.localStorage.setItem("share", "xxx");}

找到CocosdLocalStorge,setItem方法,修改如下,如果关键词是share,开启分享意向;同时value里面也可以放入要传递的值;CocosdLocalStorge本来是放在cocos2dx-android.jar里面的,现在把它公开出来了

public static void setItem(String key, String value) {        try {            String sql = "replace into " + TABLE_NAME + "(key,value)values(?,?)";            mDatabase.execSQL(sql, new Object[]{key, value});            if (key.equals("share")) {                System.out.println("jump share");                onClickShare(value);            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void onClickShare(String score) {        String shareText = "welcome to use cocos2d-x editor!";        Intent intent = new Intent(Intent.ACTION_SEND);        intent.setType("text/plain");        intent.putExtra(Intent.EXTRA_SUBJECT, "share");        intent.putExtra(Intent.EXTRA_TEXT, shareText);        context.startActivity(Intent.createChooser(intent, "share to"));    }

然后我们要从android应用里面跳到cocos2d-x游戏里面,其实很简单,就是普通的活动意向跳转,在AndroidMainActivity.java加入代码;

Button btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                startActivity(new Intent(AndroidMainActivity.this, Cocos2dMainActivity.class));            }        });

最后别忘记在AndroidManifest.xml里面把需要的参数配置好,运行正常没问题,有问题可自行调试;