(IF)Z
Indicates a method in which the parameter is an int and a float, and the return value is boolean.Now I have some understanding. the symbol in the brackets indicates the parameter type, and the symbol behind the brackets indicates the type of the return value. Because Java allows function overloading, there can be multiple methods with the same name but different return values. The method signature is used to help distinguish these methods with the same name.
Currently, the Java type signatures supported in the Cocos2d-js are as follows:
Java type |
Signature |
Int |
I |
Float |
F |
Boolean |
Z |
String |
Ljava/lang/String; |
ParametersThe parameters can be 0 or any number. You can directly use number, bool, and string in js.
ExampleWe will call the static method in the Test class above:
// Call the hello method jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "hello", "(Ljava/lang/String;) V", "this is a message from js "); // call the First sum Method var result = jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "sum", "(II) I", 3, 7); cc. log (result); // 10 // call the second sum Method var result = jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "sum", "(I) I", 3); cc. log (result); // 5
There will be correct output in your console, which is simple.
Note:Note that in android applications, cocos rendering and js logic are performed in the gl thread, android UI updates are performed in the app ui thread. Therefore, if the Java method we call in js has any UI refresh operations, they must be performed in the ui thread.
For example, in the following example, we will call a Java method, which will pop up an Alert dialog box for android.
// Add something to the familiar AppActivity class. public class AppActivity extends Cocos2dxActivity {private static AppActivity app = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); app = this;} public static void showAlertDialog (final String title, final String message) {// runOnUiThread app must be used here. runOnUiThread (new Runnable () {@ Override public void run () {AlertDialog alertDialog = new AlertDialog. builder (app ). create (); alertDialog. setTitle (title); alertDialog. setMessage (message); alertDialog. setIcon (R. drawable. icon); alertDialog. show ();}});}}
Then we call
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "title", "hahahahha");
In this way, you can see an android native Alert dialog box.
Add more materialsNow we can call Java from js. Can this problem be reversed? Of course!
Your project contains Cocos2dxJavascriptJavaBridge, which hasevalString
Method can execute js code, which is located inframeworks\js-bindings\bindings\manual\platform\android\java\src\org\cocos2dx\lib
Folder. We will add a button to the Alert dialog box and execute js in its response. In contrast to the above situation, this execution of js Code must be performed in the gl thread.
AlertDialog. setButton ("OK", new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int which) {// The app must be executed in the GL thread. runOnGLThread (new Runnable () {@ Override public void run () {Cocos2dxJavascriptJavaBridge. evalString ("cc. log (\ "Javascript Java bridge! \")");}});}});
In this way, after clicking the OK button, you should be able to see the correct output in the console. EvalString can execute any js Code and it can access your js Code objects.
Above from http://www.cocos2d-x.org/docs/manual/framework/html5/v3/reflection/zh
Add more materials by cnmm22:
When java calls js
Cocos2dxJavascriptJavaBridge class not found
If you use the cocos command line, you should add the index of the Cocos2dxJavascriptJavaBridge class. Search for Cocos2dxJavascriptJavaBridge. java and add it to the cocos2d. lib directory.
Use cocos compile-p android
Cocos2dxJavascriptJavaBridge. java in frameworks \ js-bindings \ manual \ platform \ android \ java \ src \ org \ cocos2dx \ lib
Copy Cocos2dxJavascriptJavaBridge. java into frameworks \ js-bindings \ cocos2d-x \ cocos \ platform \ android \ java \ src \ org \ cocos2dx \ lib
AppActivity. java
Import org. cocos2dx. lib. Cocos2dxJavascriptJavaBridge;
Compile succeeded.
After Cocos2dxJavascriptJavaBridge is added, no error is reported, but the data in cc. log () is not output in the console during running?
Js cannot be output in ADT.