1, JS 与 C++ 互调
调用的时候需要注意: 启动xcode 或者vs,为什么要这样呢, 是因为要C++ 要注册jsb 文件。
利用 引擎提供的jsbing生成工具, 新建MyJSBing类
#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(int data)
{
cclOG("printClass data: %d",data);
int a = 1;
int b = 2;
/*
* @ 类.函数(参数1, 参数2)
*/
bool ret = Scriptingcore::getInstance()->evalString("sdkmanage.cpp_callback(404,3)"); //C++ 调用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;
//这里一定要使用runOnUiThread
}
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
/*
*注意 另外有一点需要注意的就是,在android应用中,cocos的渲染和js的逻辑是在gl线程中进行的,而android本身的UI更新是在app的ui线程进行的,
* 所以如果我们在js中调用的Java方法有任何刷新UI的操作,都需要在ui线程进行。
*jsb.reflection.callStaticmethod(className,methodName,methodSignature,parameters...)
* */
//调用hello方法
jsb.reflection.callStaticmethod("org/cocos2dx/javascript/AppActivity","hello","(Ljava/lang/String;)V","this is a message from js");
//调用第一个sum方法
var result1 = jsb.reflection.callStaticmethod("org/cocos2dx/javascript/AppActivity","sum","(II)I",3,7);
cc.log("result sun(3,7): ",result1); //10
//调用第二个sum方法
jsb.reflection.callStaticmethod("org/cocos2dx/javascript/AppActivity","(I)V",3);
3, JS 与 OC 互调
新建一个MyJSBOcclass.h,MyJSBOcclass.mm文件(为啥要是.mm文件呢, 这里是因为 oc 代码与c++代码混合编译。)
// // 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 调用 js
js到OC的反射仅支持OC中类的静态方法。
方法名比较要需要注意,我们需要传入完整的方法名,特别是当某个方法带有参数的时候,你需要将他的:也带上。根据上面的例子。此时的方法名字是callNativeUIWithTitle:andContent:,不要漏掉了他们之间的:。
如果是没有参数的函数,那么他就不需要:,*/
//cc.log("jsb.reflection.callStaticmethod") //三个参数
//var ret1 = jsb.reflection.callStaticmethod("NativeOcclass",// "callNativeUIWithTitle:andContent:and3Content:",// "cocos2d-js",// 55,// "___+++___Test");
//cc.log("ret1: ",ret1);
//
//var ret2 = jsb.reflection.callStaticmethod("NativeOcclass",//一个参数
// "callNativeUIWithTitle:",// "cocos2d-js_one Data");
//cc.log("ret2: ",ret2);
//
//
//var ret3 = jsb.reflection.callStaticmethod("NativeOcclass",//无参数
// "callNativeUIWithTitle");
//cc.log("ret2: ",ret3);
#import <Foundation/Foundation.h>
#import "MyJSBOcclass.h"
#include "cocos2d.h"
#include "cocos/scripting/js-bindings/manual/Scriptingcore.h"
USING_NS_CC;
USING_NS_CC_MATH;
@implementation 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 调用 js
*/
Scriptingcore::getInstance()->evalString("testClass.testFunction(3,4)");
return 999;
}
+(int)callNativeUIWithTitle:(Nsstring *) oneData {
NSLog(@"oneData: %@",oneData);
return 123;
}
@end