cocos2d for iPhone

A fast, easy to use, free, and community supported 2D game engine

Register or log in - lost password?
  • Blog
  • Store
  • Games
  • Documentation
  • Download
  • About

cocos2d for iPhone » Programming » JavaScript Bindings

cocos2d Javascript bindings

(221 posts) (35 voices)
  • Started 1 year ago by riq
  • Latest reply from riq

Tags:

  • bindings
  • cheap watches sale
  • discount wedding dresses
  • halloween
  • javascript
  • javascript ccdrawline
  • javascript html5
  • replica watches for sale
  • watches replica
« Previous1…5678Next »
  1. riq
    Key Master

    @noocell @DanieL:
    Yes, It should be possible to create JSB for Facebook Connect, Game Center, etc.
    The script that generates the bindings is generic enough to support "any" Objective-C/C projects... ( For the moment it parsed cocos2d, Cocosbuilder reader, cocosdenshion and Chipmunk without problems. )

    the instructions to generate bindings are here:
    https://github.com/zynga/jsbindings#readme

    @jason / noocell:
    It is planned to add that feature. For the moment what you have to do is something similar to what I am doing for the cocos2d-html5 tests.

    1. Place all your JS files in one directory
    2. Place all your resources in another directory
    3. Include them in your Xcode project and in your "HTML5 project"
    3. Create a "bootstrap" for JSB
    4. Create a "bootstrap" for HTML5
    5. Run them.

    As an example, download cocos2d-html5 from github and cocos2d develop-v2 branch from github too, including its submodules and you will see that the code and resources are shared.

    Examples:
    bootstrap for JSB:
    https://github.com/ricardoquesada/cocos2d-html5-tests/blob/master/src/tests-boot-jsb.js

    bootstrap for HTML5:
    https://github.com/ricardoquesada/cocos2d-html5-tests/blob/master/cocos2d.js

    Posted 7 months ago #
  2. noocell
    Member

    I can't make it work for Watermelon With Me (...take the JSB codes and make it run as HTML5, just with stating the right files in appdelegate.js and cocos2d.js which I have copied from riq's example above). Native html5 helloworld example is running fine, so the problem is not in cc2d files.

    What would be openminding for me is a hint on how to make the helloworld example of Cocos2d-HTML5 project to compile properly with JSB for iphone (because HTML5 --> JSB is I think a more convenient way for someone to work on Javascript code).

    Posted 7 months ago #
  3. riq
    Key Master

    @noocell:
    Watermelon With Me it is not "web" compatible yet. It is using certain features of cocos2d like CCDrawNode and CCPhysicsDebug that were merged into cocos2d-html5 yet.

    Things that are "certified" multi-platform:
    - MoonWarriors (although it is using an "old" version of cocos2d jsb / cocos2d html5)
    - The following tests:
    https://docs.google.com/spreadsheet/ccc?key=0AtMnlkzywt1zdHlZcVZQZlp6RHhZd0lHcGtleXV4aUE#gid=1

    This is Work in Progress, so it is somewhat incomplete:
    - Most probably you will find more bugs than in cocos2d-iphone
    - There is almost no documentation
    - cocos2d-html5 has no "installer"
    - Not all cocos2d-iphone features are present...

    But being said that, all the feedback is more than welcome. If you need help with anything, just let me know.

    And since this is a work in progress, I suggest trying the github version of cocos2d-iphone and cocos2d-html5, including the tests.
    And if you want to try the "unstable" branch, try my repos:
    https://github.com/ricardoquesada/cocos2d-html5
    https://github.com/ricardoquesada/cocos2d-html5-tests
    Those two repos are usually one day ahead of the official repos. Every JavaScript API incompatibility that I find, I commit the fixes in my branches, and then send a pull request to the cocos2d-html5 guys.

    Posted 7 months ago #
  4. noocell
    Member

    @riq

    Thanks for the awesome feedback. I'll be around asking questions and waiting for the project to be more polished. It's really exciting as a potential and intrigues me to start replacing my current setup (Corona SDK) and invest more time on JSB :)

    Posted 7 months ago #
  5. Jason A
    Member

    @riq Thanks for the info, a few more questions:

    re: the spidermonkey/jsb version of the project: Is there a way to enumerate the properties of the 'cc' object?
    I'm not familiar with spidermonkey internals, but it looks like the classes generated by jsb aren't enumberable,
    i.e. (for(var k in cc){ cc.log(k); }) won't print out "Sprite", "Director" etc... I ask because I've been running into lots
    of little incompatibilities, and it would greatly simplify things to be able to just run a sanity check on each implementation
    that checks for the existence/difference of all names defined under the 'cc' namespace and the names of methods etc.

    second, the javascript representation of CGRect differs between html & jsb, and the functions for manipulating them are missing or named differently between the two platforms. I've got my own little temp shim code for working with them as opaque objects, but was wondering where the right place to raise the issue is.
    would that be html5-tests?

    if someone can show me a way to access all the properties of cc and cc.Sprite etc in js via (for ... in) or similar on the jsb side I'd be happy to
    contribute some kind of simple API-diff script for testing, as I'll be writing one for my own sanity anyway :)

    Posted 7 months ago #
  6. riq
    Key Master

    @Jason A

    if someone can show me a way to access all the properties of cc and cc.Sprite etc in js via (for ... in) or similar on the jsb side I'd be happy to
    contribute some kind of simple API-diff script for testing, as I'll be writing one for my own sanity anyway :)

    Thanks!.

    Yes, the rect API is something that we haven't unified yet. The way to create a rect is cc.rect(x,y,w,h), but the functions to manipulate a rect are not unified.
    Most probably we are going to add free functions for that (instead of a class).

    Regarding how to enumerate a class.... let me check.

    Posted 7 months ago #
  7. riq
    Key Master

    @JasonA:

    Yes, you can dump the class properties without problem when using JSB

    var scene = cc.Scene.create();
    for( var i in scene )
        cc.log( i + " = " + scene[i] );

    UPDATE:
    But I tried to iterate over cc and I couldn't enumerate the classes... Not sure why

    Posted 7 months ago #
  8. Jason A
    Member

    @riq it looks like the call to JS_InitClass doesn't create an enumerable property. A little googling hasn't really
    turned up much. I tried inserting

    jsval xxx;
    JS_GetProperty(cx, globalObj, "Sprite", &xxx);

    JS_DefineProperty(cx, globalObj, "Sprite", xxx, NULL, NULL, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);

    at the end of JSB_CCSprite_createClass but it didn't seem to do anything.

    Posted 7 months ago #
  9. Jason A
    Member

    wouldn't you know it, immediately after posting I found JS_SetPropertyAttributes looks like it'll do the trick.

    Posted 7 months ago #
  10. Jason A
    Member

    @riq adding
    JSBool found;
    JS_SetPropertyAttributes(cx,globalObj,name,JSPROP_ENUMERATE| JSPROP_READONLY | JSPROP_PERMANENT,&found);
    at the end of a JSB_*_createClass function seems to fix it.

    Posted 7 months ago #
  11. riq
    Key Master

    @Jason:
    fixed. Try the new version from JSB.
    the fix was (thanks @rolando)

    JSBool found;
    	JS_SetPropertyAttributes(cx, globalObj, name, JSPROP_ENUMERATE | JSPROP_READONLY, &found);

    Update: I have just seen that you posted the same answer. Thanks!

    Posted 7 months ago #
  12. Jason A
    Member

    @riq lol

    Posted 7 months ago #
  13. Jason A
    Member

    okay, I've hacked something to diff the APIs,
    it requires node.js. pretty primitive, but it works:

    https://github.com/jaeschliman/cocos-api-diff

    Posted 7 months ago #
  14. riq
    Key Master

    Thanks Jason.

    Posted 7 months ago #
  15. Seasons
    Member

    Hi @riq

    I've been reading cocos2d jsb on github and have a question.
    Recently jsb supports callback function with return value.

    e.g. ccTouchBegan.

    ccTouchBegan is implemented in js_bindings_cocos2d_ios_classes.mm as hook method by using JRSwizzle.
    Why just use swizzle on ccTouchBegan ? This callback function has return value ?
    Other callback functions( e.g. ccTouchMoved,Ended ) are implemented by objective-c category.

    I guess that have a same situation as onEnter callback and so on.

    Posted 7 months ago #
  16. Seasons
    Member

    It was answered my own question.

    Posted 6 months ago #
  17. Seasons
    Member

    I have a question.

    Why did change callback arguments specification?
    https://github.com/ricardoquesada/jsbindings/commit/6473649ec38365602c8601a15bb36f963f863100

    What are benefits that you change it.

    Posted 6 months ago #
  18. riq
    Key Master

    @seasons:

    Before it was using an objective-c way of passing callback functions:

    // old way: not JS friendly
    item.setCallback( this, this.callback );
    // new way: JS friendly
    item.setCallback( this.callback, this);  // but 'this' is optional.
    
    // this is also valid
    item.setCallback( this.callback); 
    
    // this is also valid
    item.setCallback( this.callback.bind(this) );  // and this one is also valid.

    So, basically the new JS API is more JS friendly. Many JS libraries use this same approach.

    Posted 6 months ago #
  19. shuvoxd
    Member

    Hello

    I am Shuvo from Bangladesh. Last few days I'm learning cocos2d javascript. And now I can move script, drag and drop things. But there is a problem. I don't find a way to add database such as sql lite in jsbinding as I have done earlier in objective c. If you can help me I'll be really grateful. And for your information I'm using cocos2d with javascript and cocos2d for iphone not cocos2d-x.

    Best regards
    Shuvo

    Posted 6 months ago #
  20. shuvoxd
    Member

    My question is for Mr.Riq........ plz help me out.

    Posted 6 months ago #
  21. Seasons
    Member

    Thanks @riq.

    Sorry, i didn't know javascrint bind function.
    Thank you for your information :) I understood it.

    Posted 6 months ago #
  22. banna31
    Member

    is this possible to add database in cocos2d Javascript bindings?I am not found any system to add database in here.Please help...........

    Posted 6 months ago #
  23. Seasons
    Member

    @banna31 @shuvoxd

    Here is measures for adding your program to javascript bindings.
    https://github.com/ricardoquesada/jsbindings/blob/master/README.md

    Do you want a sample code?

    Posted 6 months ago #
  24. banna31
    Member

    Thanx for ur reply.
    yes i need a sample code

    Posted 6 months ago #
  25. shuvoxd
    Member

    Thnx @seasons.... any sample code will be very helpful..... thnx again.. :)

    Posted 6 months ago #
  26. shuvoxd
    Member

    Hello @seasons

    Thnx again for your help.. But I'm actually finding a way add database in my game by using cocos2d iphone with javascript.... Such as sqlite database... Can I add this using js. I think now you get my point.

    Best regards
    Shuvo

    Posted 6 months ago #
  27. shuvoxd
    Member

    I am using cocos2d version 2.1 beta for your information............. Thnx

    Posted 6 months ago #
  28. banna31
    Member

    I am trying to add sqlite database in cocos2d javascript V2.1 .Is there any possible way?please help me to find out this.........

    Posted 6 months ago #
  29. riq
    Key Master

    @banna31 @shuvoxd:
    cocos2d doesn't come with pre-generated JS bindings for sqlite. You have to generate them by yourselves.

    As @seasons said, the instructions to generate them are here: https://github.com/ricardoquesada/jsbindings/blob/master/README.md

    If you have any doubt with the instructions, let me know.

    Posted 6 months ago #
  30. shuvoxd
    Member

    Thnx riq.. Is there any way to create object of different class and access all the functions of that particular class in js binding?? @riq

    Posted 6 months ago #

RSS feed for this topic

« Previous1…5678Next »

Reply »

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.