[Tutorial] Google Play Game Services with Cocos2d-js and Android
In this post, I’m going to show you how to use Google Play Game Services with your Cocos2d-js or Cocos2d-html5 game! I’m not going go show you how to use it with iOS, this tutorial is only about Android.
Let’s take a look!
I - About Cocos-js
It’s my favorite game engine! And now, Cocos support 3D game development! Ok, let’s return to the tutorial…
You can read this forum post, it’s my source to this post, but I updated some steps :)
II - Creating a Google Play Game Services project
Open the https://play.google.com/apps/publish/ go to Game Services page and add a new app.
Now, select I don’t use any Google APIs in my game yet and add the necessary info for your project.
Now, download the BaseGameUtils provided by Google in this link.
III - Preparing your project
You’ll need to add the android-support-v4.jar and google-play-services.jar to you project folder.
You can get these libs at:
Copy these .jars to your_project_folder/frameworks/runtime-src/proj.android/libs.
Get the project id bellow your project name:
Now, in your AndroidManifest.xml, add the following meta tag:
Just build and ruin you app now to check if there is no incompatibility with these new libs and some other lib that you should be using.
If it’s building, you can go to the next step :)
IV - Linking BaseGameUtils with Cocos2d-js
You can get all files on android-basic-samples-master/BasicSamples/libraries/BaseGameUtils/src/main/java/com/google/example/games/basegameutils
Update the package of all .java to:
Add the following import to your GameHelperUtils.java and BaseGameUtils.java:
Just make sure to change use your Android package name!
Open you BaseGameActivity.java and add an import and update the class definition to:
Now you can add your own methods and features in the AppActivity.java.
Check this gist to read my AppActivity, you can copy it and just change your package name and LEADERBOARD_ID.
Edit the file at /frameworks/runtime-src/proj.android/res/values/strings.xml and add some new values:
V - Linking your app
Go to Google publish, Linked apps and add a new Android project. Use the correct package name and SHA1.
Build and run your project, if everything is working, you’ll be able to login with Google :)
VI - Calling Java methods
Now if your Google Game Services is working as expected, you can call Java methods.
You can use it to list friends, show the leaderboard, add achievements, etc.
To show the Leaderboard, for example, use this JS method:
That’s it :)
If you have any problem, feel free to comment here :)
Thx for reading,