(Editing) How to integrate “Facebook” and use it in cocos2d-x2.2.2/3.0beta2version (Tutorial1: for Android )Part3

This is third part of the tutorial series. In this part, it will walk you through the process of integrating “Facebook” step by step. There are some differences between Cocos2d-x 2.x and Cocos2d-x 3.0 with integrating “Facebook”.

Downloading and Installing:

You can download cocos2d-x v3.0beta from : http://www.cocos2d-x.org/download

The architecture and API is many difficult from v2.2.2/v3.0beta

The biggest change is v3.0: Changing namespace from “cocos2d” to “cc”

How to install it :

If you don not familiar with how to install it. just can reference “How to create a “Hello World” C*+ project with cocos2d-x2.2.2 or 3.0beta”
http://www.cocos2d-x.org/forums/6/topics/43031

You can download FacebookSDK from : https://developers.facebook.com/docs/android/

Create a Facebook App

Access to Facebook Developers site, on the top bar, go to ‘Apps’ > ‘Create a New App’:

And enter basic information such as its name and a unique namespace:

Once created, note down the app ID shown at the top of the dashboard page. You’ll need to add this to your project files.

Go to ‘Setting’ in the left nav , then click on the ‘Add Platform’ button and select Android. Add the debug key hash that you obtained. And check “Single Sign On”

If you have a release key, you will need to add its hash in the same way.
To obtain the release key hash, On OS X, run:

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

On windows ,Use:

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

You will be prompted for a password. This should be ‘android’ without quotes.

You can get more information : https://developers.facebook.com/docs/android/getting-started

Integrate Facebook’s SDK in Eclipse

Before you Integrate Facebook’s SDK in Eclipse, you’ll move the SDK to the your cocos2d-x project root path “/YOUR_PROJECT/”

So let’s Open Eclipse and import 3 projects.

1, facebook-android-sdk:
under the path “/FacebookTutorialV3/facebook”

2, proj.android:
under the path “/YOUR_PROJECT/proj.android”

3, libcocos2dx:
under the path"/cocos2d-x-3.0beta2/cocos/2d/platform/android/java"

Then, make sure your “facebook-android-sdk” is a library for main project.

Now you have been integrated facebookSDK to your android projects. Go ahead and build and run the template as-is. If all works OK, you should see the following:

Facebook Plugin

You can download Facebook Plugin from :https://github.com/Ryeeeeee/FacebookTutorialV3.0

This is a simple Plugin we encapsulated method as following:

1.login
2.logout
3.getLoginStatus
4.sendRequests
5.pickFriends
6.postStatus

Move following files(.java) to “/YOUR_PROJECT/proj.android/src/org/cocos2dx/cpp”

Now we need to know how to connect “FacebookPlugin” to your “Main.Activity”.

Here is your “Main.Activity”, it dependent to What name you created the game.

Open your the AndroidManifest.xml and add following code:

Move “pick_friends_activity.xml” to “res/layout/”

Add the following code into “res/values/strings.xml”

Add the following code into “jni/android.mk”:

In this step, you’ll add code to implement some features.If you want to integreate the plugin you want, just add the plugin into your activity.For example, the followings show how to add Facebook Login Plugin. You can add other plugins in the same way.

Define a private variable for the FacebookLoginPlugin instance:

private FacebookConnectPlugin facebookLoginPlugin = null;

Next, override the onCreate() method to create a new FacebookLoginPlugin object, passing in the Context object.

@Override
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		facebookLoginPlugin = new FacebookConnectPlugin(this);
		facebookLoginPlugin.onCreate(savedInstanceState);		
}

To ensure that the sessions are set up correctly, your activity must override the activity lifecycle methods:onCreate(), onResume(), onActivityResult(), onPause(), onDestroy() and onSaveInstanceState() and call the corresponding FacebookLoginPlugin methods.

Add the following code to make those changes:

@Override
public void onResume() {
	super.onResume();
	facebookLoginPlugin.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	facebookLoginPlugin.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onPause() {
	super.onPause();
	facebookLoginPlugin.onPause();
}

@Override
public void onDestroy() {
	super.onDestroy();
	facebookLoginPlugin.onDestory();
}

@Override
public void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	facebookLoginPlugin.onSaveInstanceState(outState);
}

Integrate Facebook in C++ side

Add ‘FacebookInterface.cpp’ and ‘FacebookInterface.h’ to ‘/YOUR_PROJECT/proj.android/src/org/cocos2dx/ccp’

Open HelloWorldScene.cpp and insert the following code:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "FacebookInterface.h"
#endif

Its easy to use Facebook Login , just call the following method. After that , the callback method CallFunctionName() will be called.You can override this method to get the result of callback.The parameter ‘cbIndex’ is a tag that will be returned to first parameter of CallFunctionName().

FacebookInterface::login(int cbIndex,const char* scope);

And you will see this if you did not install Facebook:


projectroot.png (57.6 KB)


src.png (83.0 KB)


mainactivity.png (72.1 KB)


androidmani3.png (267.9 KB)

@zyy8687 i followed your tutorial for cocos2d-x 3.0.

FacebookInterface::login(int cbIndex,const char *scope);
FacebookInterface::logout(int cbIndex);

are working fine but when i try

FacebookInterface::pickFriend(4);
FacebookInterface::postStatus(3,
            "Facebook SDK for Android",
            "Build great social apps and get more installs.",
            "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.",
            "https://developers.facebook.com/android",
            "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); 

app crash with nullPointerException. Can you please help me?