如何在cocos2d-x中从C ++调用javascript函数

问题描述:

在我的cocos2d-x js项目中,我使用cxx-generator将c ++函数绑定到js,以此方式,我创建了一个ios alertView并将其显示为我的js代码,但是当用户按下OK按钮时,我可以通过事件现在到js,我尝试并用了整整2天的时间搜索了google,但是我做不到,如果有人知道解决方案是什么,请帮助我,非常感谢!

In my cocos2d-x js project, I bind c++ functions to js using cxx-generator, in this way, I create an ios alertView and show it form my js code, but when user press OK button, I can pass the event to js now, I tried and googled all the 2 days, but I can not do it, if anyone knows what the solution is, please help me, thanks very very much!

您没有提供任何代码,因此很难为您提供具体帮助,但是类似这样的东西应该提供一些指导:

You didn't provide any code, so it's hard to give you specific help, but something like this should provide some direction:

Poo.h

class JSObject;
class Poo : cocos2d::CCNode {
  public:
    static void hello(JSObject *target, std::string selector);
}

Poo.cpp

Poo::hello(JSObject *target, std::string selector) {
    if (target) {
        js_proxy_t * p;
        JS_GET_PROXY(p, target);
        if (!p) {
            return;
        }

        jsval retval;
        jsval dataVal = std_string_to_jsval(ScriptingCore::getInstance()->getGlobalContext(), "Hello World");

        ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), selector.c_str(), 1, &dataVal, &retval);
    }
}

然后,在您的JS文件中:

Then, in your JS file:

var Demo = cc.Node.extend({
  ctor: function() {
    this._super();
    // The usual init stuff
    Poo.hello(this, "myCallback");
  },

  myCallback: function(msg) {
    cc.log("I got a message back from C++: " + msg);
  }
});