Preface:
In this section, the author will add google AdMob ads to flappy, which is also the secret of why Vietnamese guys are about to enter 50 thousand US dollars every day. The platform is still on android phones. Through this demo, readers can learn how JavaScript code interacts with android code, how to press the button to detect and exit, and how to share it;
If no advertisement is displayed, check whether wifi is enabled and whether the anti-virus security software prohibits the advertisement module;
Background:
I was amazed by the recent popular global game flappy bird, so it took me a day to copy the same game. Now I share the idea and source code of the game. The Code is based on the javascript language, cocos2d-x game engine, cocos2d-x editor mobile game development tools completed, please the reader light hit;
Ps: To run the demo, You need to configure the cocos2d-x editor. Other tools are not currently supported. In addition, the demo is cross-platform and can be transplanted to run android, ios, html5 webpages, etc.
Android source code:
Go to code concentration camp download (flappy source code -- add admob ads): http://blog.makeapp.co /? P = 306 & preview = true
(There is advertisement on the top of the picture)
Code Analysis
First, implement the advertisement module: add GoogleAdmobAdsSdk. jar to the previous android code, copy it to the libs path, and right-click add as library;
Then add the plugin module to the src directory.
Next, modify MainActivity. js in asserts and add the advertisement code to the onEnter method. a152fcf037adb43 is the advertisement key, which can be applied on the admob official website. showAds indicates the position of the advertisement;
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"); }
Find the Cocos2dMainActivity file. The entire game scenario exists as an activity. modify the code as follows. initialize PluginWarpper and add the onBackPressed () event to return the key event. System. loadLibrary ("cocos2dx-game") is also very critical, the need to download the library; In AndroidManifest modify the corresponding parameters, the Code is not listed; run can see the advertisement, you can also click back to bring up the exit box;
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 struct agetype = getResources (). getConfiguration (). locale. getCountry (); if (aggregate agetype. equals ("C N ") {Cocos2dxLocalStorage. setItem ("language", "CN"); System. out. println ("language china");} else {Cocos2dxLocalStorage. setItem ("language", "US"); System. out. println ("language english");} */} public Cocos2dxGLSurfaceView onCreateView () {export glSurfaceView = new Cocos2dxGLSurfaceView (this); // HelloAds shocould create stencel buffer surglfaceview. 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 Diener, int which) {}}); AlertDialog dialog = builder. create (); dialog. show ();} public void onClickShare (View view) {Intent intent = new Int Ent (Intent. ACTION_SEND); intent. setType ("image/*"); intent. putExtra (Intent. EXTRA_SUBJECT, "share"); intent. putExtra (Intent. EXTRA_TEXT, "now !!! "); Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (Intent. createChooser (intent, getTitle ();} static {// System. loadLibrary ("game"); System. loadLibrary ("cocos2dx-game ");}}
Then we trigger the sharing event in the game and find asserts -- MainLayer. js, add a click event to the 123 image; sys. localStorage. setItem ("share", "xxx") is a communication channel. The keyword share is obtained and directly triggered by CocosdLocalStorge setItem;
MainLayer.prototype.onGradeClicked = function () { sys.localStorage.setItem("share", "xxx");}
Find CocosdLocalStorge, setItem method, modify the following, if the keyword is share, enable sharing intention; also value can also be placed in the value to be passed; CocosdLocalStorge is originally placed in the cocos2dx-android.jar, now we have made it public.
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")); }
Then we want to jump from the android Application Inside the cocos2d-x game, in fact, very simple, is the general activity intention jump, in AndroidMainActivity. java to add code;
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)); } });
Finally, do not forget to configure the required parameters in AndroidManifest. xml. The operation is normal and there is no problem. If there is any problem, debug it on your own;
Flappy blog series:
Flappy bird game source code revealing and downloading
Flappy bird game source code exposure and download follow-up-porting to android real machine
Flappy bird game source code exposure and download follow-up-porting to html5 Web Browser
Flappy bird game source code revealing and downloading follow-up-the secret of about $50 thousand daily AdMob ads
Flappy bird game source code exposure and download follow-up-transplanted to Apple ios (not written)
Author's note:
For more information, please go to the official blog. The latest blog and code will be launched on the official blog. Please stay tuned for more information. The source code of the cocos2dx editor game will be released soon;
Contact me: zuowen@makeapp.co (Mailbox) QQ group: 232361142
Postscript:
In order to encourage more mobile game enthusiasts to join the cocos2d-x editor camp, but also to the reader's strong confidence in the tool, the author based on the above flappy bird code and new image resources, A similar game, fly bird, was released to google play and the Chinese app market ). The game is also added to the advertising module to get some advertising fees. I also hope that readers can publish more good games in the market through cocos2d-x editor;
Google play: https://play.google.com/store/apps/details? Id = com. makeapp. game. flybird (TIPS: If you change the suffix of application .apkto .rar, you will get unexpected things.)
Pods market address: http://www.wandoujia.com/apps/com.makeapp.game.flybird
Machine front market address: http://apk.gfan.com/Product/App741996.html
Html5 Online Demo: http://www.makeapp.co/flybird/ (Microsoft IE browser compatibility problems, please choose other browsers; company intranet, small card, forgive me)