cocos2dx-js(3.14版本) JS与C++ 互调 JS与JAVA 互调,JS与OC 互调

前端之家收集整理的这篇文章主要介绍了cocos2dx-js(3.14版本) JS与C++ 互调 JS与JAVA 互调,JS与OC 互调前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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
原文链接:https://www.f2er.com/cocos2dx/338883.html

猜你在找的Cocos2d-x相关文章