4

I'm researching the possibility of using cocos2d-js by embedding it as a view inside an existing iOS app. In order to make this work, I'm going to need 2-way communication between cocos2d and the surrounding application.

After some initial investigation, I have determined that it is possible to call in to cocos using ScriptingCore:

ScriptingCore* sc = ScriptingCore::getInstance();
jsval outVal;
sc->evalString("function()", &outVal);

My question, then, is around doing the reverse. It is possible to (e.g. in response to user input) call back out of cocos2d-js to C++? Ideally, there would be a way to register a callback with ScriptingCore which could be invoked from JavaScript.

Derek Thurn
  • 14,953
  • 9
  • 42
  • 64

1 Answers1

1

I believe it can be done, but I have not tried myself, nor can I find a good and concise example. All I can do is point you at SuperSuraccoon's Bluetooth example and it's git page, which apparently does both ways communication between C++ and JS code.

Sebastián Vansteenkiste
  • 2,234
  • 1
  • 19
  • 29
  • 1
    Yes, it looks like the key is to call [JS_DefineFunction](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_DefineFunction). The SpiderMonkey JSAPI reference has a lot of good information on how to do this kind of thing, actually, I just wasn't looking in the right place before. – Derek Thurn Jun 03 '14 at 23:27