Because Apple does not allow apps to download executable code, it cannot use a dynamic link library to build a plug-in engine and download it over the Internet on iOS. However, one way is to integrate a script interpreter inside the engine and use the script as a resource to download the script encrypted), so as to circumvent Apple's review terms. This method is called Hybrid. However, the reason why Hybrid does not reveal traces is that Hybrid is too profitable, and Apple may close one eye.
Among the many branches of cocos2d engine, the development of cocos2d-x is C ++ as the core. The cocos2d-x engine is to execute js Code through hybrid. To execute js Code, the engine itself needs a script interpreter. The engine-integrated script interpreter is called spidermonkey.
Spidermonkey is a long-established js script interpreter based on c/c ++. It is provided by Mozilla and is widely used in firefox and thunderbird. Cocos2d-x integrated spidermonkey open source protocol is MPL2.0, there is no limit, you can rest assured to use it.
In the AppDelegate: applicationDidFinishLaunching () function, we can find the code to start the script engine:
- ScriptingCore* sc = ScriptingCore::getInstance();
- sc->addRegisterCallback(register_all_cocos2dx);
- sc->addRegisterCallback(register_cocos2dx_js_extensions);
- sc->addRegisterCallback(register_CCBuilderReader);
- sc->addRegisterCallback(jsb_register_chipmunk);
- sc->start();
-
- CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
- CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
- ScriptingCore::getInstance()->runScript("MoonWarriors-jsb.js");
ScriptCore is the core of the script. It is the JS interpreter we mentioned. The cocos2d-x encapsulates the interpreter of spidermonkey to provide support for the cocos2d-x engine and simplify the Calling Interface.
AddRegisterCallback interface is used to add registration functions. Registration functions are used to bind the corresponding code to the ing code from JS to C ++ during engine execution ). Each register function corresponds to a library. Now the cocos2d-x provides four database support, namely cocos2d-x core library, cocos2d-x extension library, cocosbuilder support library, clipmunk physical engine library. In the future, you can add and register your own JS binding library here to directly expand this JS engine.
Start to start the script engine.
CCScriptEngineManager: sharedManager ()-> setScriptEngine: binds the script engine to the engine Manager. The engine manager provides a global access point for the script engine, it is also responsible for uninstalling the script engine.
The last step is to run the main script of the game.
ScriptingCore: getInstance ()-> runScript ("MoonWarriors-jsb.js ");
This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/1120219