如何从objective-c调用cocos2d-js方法

时间:2014-12-01 08:57:33

标签: objective-c cocos2d-x cocos2d-js

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

我知道来自cocos2d-js的call objective-c就像: 在js:

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

但是,如何从objective-c ...

调用cocos2d-js

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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);
}