Part 1
Java calls c ++, refresh
First, we will use engine 2.2.1 as an example to create a new project named TestJni.
The creation method is as follows:
-Language cpp
Http://blog.csdn.net/liuyuyefz/article/details/9129525
Our first goal is to send signals to the C ++ layer through the java layer, and then let the CCLabelTTF in our cocos2d-x's HelloWorld project change the display content.
Please believe that I only need a few simple steps. Then the miracle will come.
Let's take a look at what the c ++ code of our familiar cocos2d-x needs to add and modify.
CCLabelTTF * pLabel = CCLabelTTF: create ("Hello World", "Arial", 24 );
This is to create a Hello World Label, which is the main character of this time.
We want to change the label value by calling a function in java. Therefore, we must redefine pLabel as a global variable.
You need to replace the above Code with the following code:
PLabel = CCLabelTTF: create ("Hello World", "Arial", 24 );
In HelloWorldScene. h
Public:
Add the following code
Cocos2d: CCLabelTTF * pLabel;
This is used to change the display content in pLabel.
Std: string HelloWorld: changeStr = ""; // Add a Global static variable to indicate the state to receive the string passed by java.
Void HelloWorld: changeText (float t) // Add a method to change the label display content
{
If (HelloWorld: changeStr. compare ("")! = 0) // when no content is transmitted in java: changeStr is blank
{
PLabel-> setString (HelloWorld: changeStr. c_str ());
HelloWorld: changeStr = "";
}
}
In HelloWorldScene. h
Public:
Add the following code
Static std: string changeStr;
Void changeText (float t );
Here you can try to see
Of
Bool HelloWorld: init (){
....
}
Add the following code at the bottom of the function and run it.
CCDirector: sharedDirector ()-> getschedctor ()-> scheduleSelector (schedule_selector (HelloWorld: changeText), this, 0, false); // Add a timer, to dynamically check each frame, it is a java text transfer.
# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID)
# Include <jni. h>
Extern
"C"
{
Void Java_org_cocos2dx_TestJni_TestJni_changeTTFLabel (JNIEnv * env, jobject thiz, jstring textStr)
{
// Here, java calls changeTTFLabel ("Test MyLabel Change! ");, Which will be called here. Jint
Const char * str;
Str = env-> GetStringUTFChars (textStr, false );
Std: string tempStr (str );
HelloWorld: changeStr = tempStr;
}
}
# Endif
Part 1:
The distribution will explain the above Code
# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID)
# Endif
The above Code defines the platform. The code between the codes is called only when the android project is compiled. This is because we cannot call the code between ios projects. Otherwise, compilation errors may fail.
Part 2:
Part 3:
Void Java_org_cocos2dx_TestJni_TestJni_changeTTFLabel (JNIEnv * env, jobject thiz, jstring textStr)
The format rule here is
Java _ signature (org_cocos2dx_TestJni _) _ Class Name (TestJni) _ changeTTFLabel (the function to be called under this class)
The following section
(JNIEnv * env, jobject thiz, jstring textStr)
The first two are required, and the last jstring textStr is the parameter to be passed.
The parameters can be jstring or jint. They correspond to the java String or int type respectively.
Part 4:
Const char * str;
Str = env-> GetStringUTFChars (textStr, false );
Convert jstring to const char * of c ++ *
We have completed all the code in the c ++ section.
Before proceeding to this step, you must configure the cocos2dx android environment compiled by all eclipse.
Refer to this article: http://blog.csdn.net/jackystudio/article/details/12419387
Then
Public class TestJniextends Cocos2dxActivity {
......
}
Add the following code in the function.
Private static native void changeTTFLabel (String textStr); // This is an intermediary interface that provides java to call the JNI function of the same name in c ++.
Finally
Protected void onCreate (Bundle savedInstanceState ){
......
}
Add the following code in the function. Call
ChangeTTFLabel ("Test MyLabel Change! ");
Compilation of c ++ code
Go
Cocos2d-x2.2.0/projects/TestJni/proj. android
Under the folder, compile
Build_native.sh
To compile c ++
In windows, you need cywin to simulate the linux environment.
Direct under mac
./Build_native.sh. The following figure shows the successful compilation. If erro exists in the compilation result, check the instructions to see where errors occur in c ++.
Then compile the java part of android in eclipse
The method is eclipse-clean
Connect to the real machine: After debugging, The result label is changed from Hello World to Test MyLabel Change!
Next: http://blog.csdn.net/liuyuyefz/article/details/17758809