COCOS2DX-JS (3.14 version) JS and C ++ intermodulation JS and JAVA intermodulation, JS and OC intermodulation

1, JS and C++ intermodulation 

When calling, you need to pay attention to: Start xcode or vs, why should this be? It is because C++ needs to register the jsb file. 

Use the jsbing provided by the engine to generate Tool, create a new MyJSBing class

# ifndef MYJSBING_H_#define MYJSBING_H_#include "cocos2d.h"#include "stdarg.h"USING_NS_CC;class MyJSBing: public Ref{public: MyJSBing(); ~MyJSBing(); static MyJSBing *getInstance(); int printClass(int data );	void callFunction();private:	static MyJSBing *_instance;};#endif
#include "MyJSBing.h"#include "scripting/js-bindings/manual/ScriptingCore.h"MyJSBing *MyJSBing::_instance = nullptr;MyJSBing::MyJSBing(){}MyJSBing::~MyJSBing(){}MyJSBing *MyJSBing::getInstance(){ if (_instance == nullptr) {_instance = new MyJSBing(); _instance->autorelease();} return _instance;}int MyJSBing::printClass(in t data){ CCLOG("printClass data: %d", data); int a = 1; int b = 2; /* * @ class. function (parameter 1, parameter 2) */ bool ret = ScriptingCore::getInstance ()->evalString("SDKManage.cpp_callback(404, 3)"); //C++ call JS return 222;}void MyJSBing::callFunction(){}

在app.js中, 调用MyJSBing.getInstance.printClass(4), 即可完成js 调用C++ 

2, JS 与Java 互调

 /* * js-> java * java->js * */ public static void hello(String msg){ System.out.println(msg);} public static int sum (int a, int b){ return a + b; //must use runOnUiThread here} public static void sum(int a){ app.runOnGLThread (new Runnable() {@Override public void run() {Cocos2dxJavascriptJavaBridge.evalString("SDKManage.javaToJSCall(\"a\", 5, 7)"); // java to js} }); }

  // js调用Java            /* *Note: Another thing to note is that in the android application, the rendering of cocos and the logic of js are performed in the gl thread, while the UI update of the android itself is performed in the ui thread of the app. * So if we are in Any operation of refreshing the UI of the Java method called in js needs to be performed on the ui thread. *jsb.reflection.callStaticMethod(className, methodName, methodSignature, parameters...) * */ //Call the hello method jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "hello", "(Ljava/ lang/String;)V", "this is a message from js"); //Call the first sum method var result1 = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sum", " (II)I", 3, 7); cc.log("result sun(3, 7): ", result1); //10 //Call the second sum method jsb.reflection.callStaticMethod("org/cocos2dx /javascript/AppActivity", "sum", "(I)V", 3);

3, JS and OC intermodulation

Create a new MyJSBOCClass.h, MyJSBOCClass.mm file (why it is a .mm file, here is because oc code and c++ code are mixed and compiled.)

//// MyJSBOCClass.h// PlatformGame//// Created by Hejz on 17/2/11.////#ifndef MyJSBOCClass_h#define MyJSBOCClass_h@interface NativeOcClass: NSObject+( int)callNativeUIWithTitle:(NSString *) title andContent:(NSNumber *)content and3Content:(NSString *)co3ntent;+(int)callNativeUIWithTitle;+(int)callNativeUIWithTitle:(NSString *) oneData;@end#endif /* MyJSBOCClass_h * /

// // MyJSBOCClass.m// PlatformGame//// Created by Hejz on 17/2/11./////* oc The reflection from calling jsjs to OC only supports static methods of classes in OC. Pay attention to the method name comparison, we need to pass in the complete method name, especially when a method has parameters, you need to bring his: also. According to the example above. The method name at this time is callNativeUIWithTitle:andContent:, don't miss the: between them. If it is a function without parameters, then he does not need:, *///cc.log("jsb.reflection.callStaticMethod") //Three parameters //var ret1 = jsb.reflection.callStaticMethod("NativeOcClass", // "callNativeUIWithTitle:andContent:and3Content:",// "cocos2d-js",// 55,// "___+++___Test");//cc.log("ret1: ", ret1);// //var ret2 = jsb.reflection.callStaticMethod("NativeOcClass", //A parameter // "callNativeUIWithTitle:",// "cocos2d-js_one Data");//cc.log("ret2: ", ret2); //////var ret3 = jsb.reflection.callStaticMethod("NativeOcClass", //No parameters// "callNativeUIWithTitle");//cc.log("ret2: ", ret3);#import #import "MyJSBOCClass.h"#include "cocos2d.h"#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"USING_NS_CC;USING_NS_CC_MATH;@implementatio n NativeOcClass+(int)callNativeUIWithTitle:(NSString *) title andContent:(NSNumber *)content and3Content:(NSString *)thridValue {NSLog(@"title: %@", title); NSLog(@"content: %d", content); NSLog(@"thridValue: %@", thridValue); return 456;}+(int)callNativeUIWithTitle {NSLog(@"void data"); /* * OC call js */ ScriptingCore::getInstance()- >evalString("testClass.testFunction(3, 4)"); return 999;}+(int)callNativeUIWithTitle:(NSString *) oneData {NSLog(@"oneData: %@", oneData); return 123;}@end 

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3683 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.