What is Cocos2d-JS? - cocos2d-x

Cocos2d-JS is Cocos2d-x engine's JavaScript version that includes
Cocos2d-html5 and Cocos2d-x JavaScript Bindings. It equips your game
with cross-browser and cross-platform abilities, accompanied by full
Cocos2d-x features and simplified JavaScript friendly APIs.
I understand that you write JS and it works every where, but how it is done? I want to understand this diagram: http://www.cocos2d-x.org/wiki/Getting_Started_Cocos2d-js As I understand cocos2d-html5 is the same thing as cocos2d-x but it is in JS and based on WebGL. If this is true then what is Cocos2d-JSB? Does it compile JS script to native code? Or is it a JS extended interpreter, that understands more than native interpreter and can interpret cocos2d specific commands?

As I understand cocos2d-html5 is the same thing as cocos2d-x but it is
in JS and based on WebGL. If this is true then what is Cocos2d-JSB?
I believe the JSB appended to cocos2d-x JSB simply means: cocos2d-x JavaScript Bindings
The module that does the translation / mediation between two programming languages is commonly called a "binding". The JSB is just a small aspect of cocos2d-x that allows users to run apps written in cocos2d-js Javascript via the cocos2d-x C++ rendering engine.
Note that cocos2d-html5 is not exactly the same as cocos2d-x. It provides similar functionality than cocos2d-x but runs only in a web browser. Obviously cocos2d-html5 has no functionality for touch or accelerometer input, and other restrictions when it comes to saving and loading files, for instance.
Does it compile JS script to native code? Or is it a JS extended interpreter, that understands more than native interpreter and can interpret cocos2d specific commands?
The JSB simply maps between the Javascript API and the C++ cocos2d-x versions of classes, methods, identifiers and what not.
I don't know whether it compiles Javascript to native code.
The gist of this:
cocos2d-js provides a programming interface (API) in Javascript that produces the same results whether you run it in the browser via cocos2d-html5 or on other (mobile) platforms via cocos2d-x.

Cocos2d-x uses SpiderMonkey, the Firefox JS virtual machine (VM), to
execute JS code.
The JS VM is extended to support all the cocos2d, Chipmunk and CocosBuilder Reader APIs. So, when you create a CCSprite in JS, you
are actually creating an C++ CCSprite. When you create an action in
JS, you are actually creating an C++ action, when you create a
particle system in JS, you are actually creating an C++ particle
system… and so on. This approach is about 10x ~ 20x faster than HTML5
games even when they use accelerators like directCanvas Basically all
the cocos2d, Chipmunk or CocosBuilder Reader APIs are going to perform
almost at native speed. But you should pay attention to the following
scenarios: The performance could be slow down while the garbage
collector is run. Workaround: Do not create many JS objects. Reuse as
many as possible Having a complex main loop could slow down the
performance. Workaround: Profile your JS code, and if you can’t
further optimize it, write the expensive parts in C++ and create JS
bindings for those functions.
This is from wiki. So the JS interpreter is extended to understand cocos2d api command such as create Action or create Sprite.

Related

Choice for multi-platform application development

I like actionscript very much and did a project with Apache Flex 4.12. Originally my mobile application was built in mxml and the performance was awful. Then I rewrite the application with single line mxml file and the rest of the project in pure as3, the performance is much better.
Now I have another project, it is an multi-platform informational application with some e-commerce capabilities target on both desktop and mobile platform and need to decide, on performance/flexibility/scalability wise, should I stick with Flex (mxml) / Flex (pure as3) / or Pure AS3 with Starling+FeathersUI+as3-signal?
I was thinking to shift into the Starling approach, however I see improvements from the Flex 4.14 release, anyone can give me a direction that which approach is more promising in long term?
Sorry for my bad English as I am not a native user. Thanks.
Please look into Haxe (www.haxe.org) and OpenFL (www.openfl.org) as an alternative to Starling.
Haxe is a programming language that is very similar to action script 3.
OpenFL - open flash library is a framework for Haxe programming language.
The benefit: of Haxe + OpenFL are:
Astounding performance
Fast and easy development
Non-existing learning curve for AS3 programmer ( you might need to spend 10 min learning about loops and casting though )
99% same API as the Flash API
I don't recommend Starling because: it's still AIR and you won't want it, the performance is below average, you'll need to re-write your whole code-base for Starling, you won't be able to port your game outside of Starling without rewriting it again and the fact that you won't be able to compile to many different platforms other than the currently supported ones.

Using JavaScript for scripting in C++ code

I'm using version 3.1.
On the internet I find various references to how JavaScript can be used in Cocos2d-x as a scripting language for rapid iteration. However, all Docs regarding this seem to be outdated, and there are only Lua samples in the Cocos2d-x test samples.
Has JavaScript usage been removed from Cocos2d-x entirely, and I now have to rely on Lua?
Otherwise, how do I bind C++ code to JS and how do I call JS code from C++?

Same code for Cocos2Dx and Cocos2d-html5?

Here I see the following:
.... the team of Cocos2d-x are working on something called "Javascript binding for Cocos2d". What that means is your very same code running on Cocos2d-html5 engine can work flawlessly on Cocos2d-X and Cocos2d-iPhone without or with little modification. And all that translates to "almost native fast on mobile phones".
What I can not understnad is that how the code written in one language can be translated to another one? Do they mean that there is a tool that gets as an input JavaScript code of Cocos2d-html5 game and translate it to C++ Cocos2dx code or backwards?
EIDT: Can I port easily the C++ game written with Cocos2Dx into Cocos2D-html5? What I have to do for that?
Cocos2d-x and Cocos2d-html5 share the same Javascript API and are written to give developer an advantage to write your code in Javascript and it will work with both of these frameworks.
As for you second question, Cocos2d-html5 runs Javascript natively whereas Cocos2d-x uses SpiderMonkey (a Javascript engine written in C/C++ by Brendan Eich) to run your Javascript code.
Although, there is one important thing to consider. Developing games using native language and APIs takes less time, easy to debug and are easily expandable and maintainable. So, in my opinion one should use cocos2d-iphone if the game is only for iOS, cocos2d-x if the game is on multi-platforms and cocos2d-html5 only if the primary target of the game is web.
It means that both cocos2d-x and cocos2d-html use the same Javascript API. So under ideal circumstances the same JS code runs on html5 and another time with the -x engine.
However there can still be differences because behind the scenes the implementation may be subtly (or not so subtly) different with each engine. Also not all features (ie touch input, accelerometer, shaders) can be available on every engine. Hence the phrasing "with little modification".
But if your main target isn't web there's little sense in using the JS API because it adds an extra layer that makes it more difficult to debug code issues, and the overall performance suffers compared to writing directly (in C++) for -x.

How to use library of other languages in Action Script 3?

Is there any way to use the library of other language in Action Script 3 ?
For C/C++
I learned that we can use Alchemy: http://labs.adobe.com/technologies/alchemy/
But for other languages (here I mean Ruby or Java), can I use their libs via AS3 ?
Alchemy is a cross-compiler from LLVM to ABC, if the language you are interested in can compile to LLVM, then, in theory, you can compile to ABC. So, Alchemy supports (with some tweaks) also C# or Java (but you will have to spend some time getting them to work together, there's no out of the box solution).
AIR (which is a desktop variant of Flash) can use something called "NativeProcess" which means that granted the operating system can execute the code in the library, then you can do so by creating a new process that calls the function from the library you need.
In AIR, again, there's a new feature called ANE (AIR Native Extensions) - this allows you to bind AIR more tightly to the native code. But, it doesn't need to be native as in object files only, it should be something the underlying system can execute. But I have to confess, I never tried creating any such extension, so my knowledge of it is theoretical.
No you can not use Ruby or Java code/libraries in Flash. Alchemy only allows you to use C and C++ libraries. The only way is to port those libraries to ActionScript, or maybe the library you want to use has already been ported to AS3.
It is also possible that there is something similar in AS3. You could ask another question here or on an Flash forum if anyone knows if the library you need has an equivalent in flash.

Execute dynamic algorithms in AS3

I have a Flash in AS3. I want to dynamically load code that performs some kind of calculations. Is it doable with AS3 (I've read that there is no eval)? Should I use an interpreter in AS3 that gets the data and does the calculations?
My first thought was to load an external swf that does all the calculations but I think having an engine that gets input from a socket is more elegant as a solution.
So, how is it possible to execute algorithms that come from a server in a swf file?
Well, there is an AS3 Eval Library by metalbot. You should probably take a look.
Also you may use browser JS engine to perform JavaScript eval with code downloaded from server. Look at ExternalInterface AS3 class for that.
If number of your code snippets is limited you could use special swfs (prepared beforehand) with those code snippets, downloaded from server. After loading an swf you can use it's codebase without any evals.