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++?
Related
I have attempted to do the following
When I tried to check either Bullet or FreeType or Both, the following pops up
I do not get why?
The Bullet and FreeType libraries both depend on native code (C or C++) that isn't available in the HTML Libgdx backend.
The HTML Libgdx backend uses Google's GWT project to cross-compile Java bytecodes into Javascript. Thus, only Java code, or APIs that exist in both places can be used. See http://www.badlogicgames.com/wordpress/?p=2308 for more details.
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.
Right now I'm using Corona SDK so I have experience with LUA but I need to implement some features in Java but I would still like to use LUA as the "main" language. Is this possible in Cocos2d-x?
Another question is if native objects will always be on top as in Corona SDK? Maybe this is not a Corona specific problem, but maybe a thing with the devices?
there is a javascript binding and Lua binding but no java check alternative game engine for java like Slick2D
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.
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.