首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从objective-c调用cocos2d-js方法

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

Stack Overflow用户
提问于 2014-12-01 16:57:33
回答 2查看 1.8K关注 0票数 1

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

我知道从cocos2d-js调用objective-c是这样的:在js中:

代码语言:javascript
复制
jsb.reflection.callStaticMethod("objective-cClass","methodName:", "parm");

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

EN

回答 2

Stack Overflow用户

发布于 2015-12-21 19:25:16

我们知道,objective-c可以很容易地调用c++方法,所以我们可以像这样使用:

代码语言:javascript
复制
jsval ret;
ScriptingCore::getInstance()->evalString("CommonFun.setDiamond(88)", &ret);

别忘了包括头文件,对于cocos2d-x 3.8:

代码语言:javascript
复制
#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"
票数 1
EN

Stack Overflow用户

发布于 2016-01-08 17:23:53

1: js如何像这样调用objc方法,js只调用静态方法

代码语言:javascript
复制
(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方法

代码语言:javascript
复制
(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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27224765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档