1

我正在使用 cocos2d-js ,这是一个购买应用程序,当购买成功时,我需要通知 cocos2d-js 更新 ui 或其他东西的消息,

我知道从 cocos2d-js 调用 Objective-c 就像:在 js 中:

jsb.reflection.callStaticMethod("objective-cClass","methodName:", "parm");

但是,如何从objective-c调用cocos2d-js ...

4

2 回答 2

1

希望对某人有所帮助,正如我们所知,objective-c 可以轻松调用 c++ 方法,因此我们可以这样使用:

jsval ret;
ScriptingCore::getInstance()->evalString("CommonFun.setDiamond(88)", &ret);

并且不要忘记包含头文件,对于 cocos2d-x 3.8:

#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"
于 2015-12-21T11:25:15.997 回答
0

1:js怎么这样调用objc方法,js只调用静态方法

(1)//NativeOcClass.mm  filename
import <Foundation/Foundation.h>
@interface NativeOcClass : NSObject
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content;
@end

@implement NativeOcClass
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alertView show];
    return true;
}
@end

(2)for js file
var ret = jsb.reflection.callStaticMethod("NativeOcClass", 
                                           "callNativeUIWithTitle:andContent:", 
                                           "cocos2d-js", 
                                           "Yes! you call a Native UI from Reflection");

2:objc调用js方法

(1)js method,e.g.//Test.js
var Test={
          myMethod:function(){
              console.log("call js");
              }
          }
(2)objc //filename.mm
#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"
....
-(void)callJsMethod
{
   jsval ret;
   ScriptingCore::getInstance()->evalString("Test.myMethod()", &ret);
}
于 2016-01-08T09:23:52.717 回答