[Cocos2d-x from c ++ to js] 03: hybrid Development Mode

Source: Internet
Author: User

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:

 
 
  1. ScriptingCore* sc = ScriptingCore::getInstance(); 
  2. sc->addRegisterCallback(register_all_cocos2dx); 
  3. sc->addRegisterCallback(register_cocos2dx_js_extensions); 
  4. sc->addRegisterCallback(register_CCBuilderReader); 
  5. sc->addRegisterCallback(jsb_register_chipmunk); 
  6. sc->start(); 
  7.  
  8. CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance(); 
  9. CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); 
  10. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.