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…678Next »
  1. riq
    Key Master

    @shuvoxd:

    Yes, jsbindings let's you bind any kind of Objective-C object in JS.
    What object do you have in mind ?

    Posted 6 months ago #
  2. shuvoxd
    Member

    Actually I am trying to code something for iPhone and as well as android in a same platform.... Thats why I choose cocos2d Jsbinding...... But I can't add database in jsbinding.... can u suggest me anything what should I do ? Thnx @riq....

    Posted 6 months ago #
  3. riq
    Key Master

    @shuvoxd:
    Please, describe your context / scenario, the things that you tried and didn't work, the documentation that you have read, etc.

    Posted 6 months ago #
  4. shuvoxd
    Member

    Thnx riq. I am a big fan of yours. First of all I am ameture in cocos2d so plz don't bother on my immature questions. I am using cocos2d 2.1 beta 2. In Xcode I code with cocos2d with javascript...
    I tried to add database with jsbinding and failed. Now I am trying to create some animation like
    http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d with JS.
    In Jstest I saw your tutorials of SpriteBatchAnimation... But I can't animate like the link... Plz..Help me out....... Thnx again.... You are my favourite.

    Posted 6 months ago #
  5. riq
    Key Master

    @shuvoxd

    I cannot help you if you don't describe the things that you have done. One more time:
    - Have you read the documentation ?
    - What have tried ? What worked and what didn't work ?
    - What DB did you add ? how did you add it ?

    Posted 6 months ago #
  6. shuvoxd
    Member

    I tried to add sqlite database in Js.... Suppose I've a DB class like DBproductInfo.m and DBproductInfo.h in objective c. when I tried to add elements i create a method in
    - (int)insertIntoDatabase:(sqlite3 *)db; in DBproductInfo.h class

    and put the method defination in DBproductInfo.m class like

    - (int)insertIntoDatabase:(sqlite3 *)db {
    database = db;

    if (insert_statement == nil) {
    static char *sql = "INSERT INTO products(productid,categoryid,name,adult_time,level_id,feed_time,breed_time,buy_coins,buy_bucks,sell_coins,tilex,tiley,reqTimeToCollectCoins,collectCoinsAmount,no_owned,animation_sequence,cross_breedable,breed_cost,experience,experience_coins,sell_bucks,sound,c3,c4,c5,c6) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
    NSLog(@"Insert");
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    }
    }
    sqlite3_bind_int(insert_statement, 1, productid);
    sqlite3_bind_int(insert_statement, 2, categoryid);
    sqlite3_bind_text(insert_statement, 3, [product_name UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_int(insert_statement, 4, adult_time);
    sqlite3_bind_int(insert_statement, 5, level_id);
    sqlite3_bind_int(insert_statement, 6, feed_time);
    sqlite3_bind_int(insert_statement, 7, breed_time);
    sqlite3_bind_int(insert_statement, 8, buy_coins);
    sqlite3_bind_int(insert_statement, 9, buy_bucks);
    sqlite3_bind_int(insert_statement, 10, sell_coins);
    sqlite3_bind_int(insert_statement, 11, tilex);
    sqlite3_bind_int(insert_statement, 12, tiley);
    sqlite3_bind_int(insert_statement, 13, reqTimeToCollectCoins);
    sqlite3_bind_int(insert_statement, 14, collectCoinsAmount);
    sqlite3_bind_int(insert_statement, 15, no_owned);
    sqlite3_bind_int(insert_statement, 16, animation_sequence);
    sqlite3_bind_int(insert_statement, 17, cross_breedable);
    sqlite3_bind_int(insert_statement, 18, breed_cost);
    sqlite3_bind_int(insert_statement, 19, experience);
    sqlite3_bind_int(insert_statement, 20, experience_coins);
    sqlite3_bind_int(insert_statement, 21, sell_bucks);
    sqlite3_bind_int(insert_statement, 22, sound);
    sqlite3_bind_int(insert_statement, 23, no_inventory);
    sqlite3_bind_int(insert_statement, 24, c4);
    sqlite3_bind_int(insert_statement, 25, c5);
    sqlite3_bind_int(insert_statement, 26, c6);

    int success = sqlite3_step(insert_statement);
    sqlite3_reset(insert_statement);
    if (success == SQLITE_ERROR) {
    NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
    primaryKey = 0;
    } else {
    NSLog(@"INserted Product Successfully...");
    primaryKey = (NSInteger)sqlite3_last_insert_rowid(database);
    }
    return primaryKey;
    }

    This works fine in Objective c. But I want create an Object of this class in JS like DBproductInfo.create().... it gives me error.. For big games I've to create database..... and I am facing problems in create DB class object in JS.... Is there any way for me?

    Posted 6 months ago #
  7. riq
    Key Master

    @shuvoxd: I am sorry, but I cannot help you. I have asked you many times these questions: http://www.cocos2d-iphone.org/forum/topic/31640/page/7#post-213946
    But so the only one that you have answered was that you are using SQLite.

    Posted 6 months ago #
  8. banna31
    Member

    how to call method from another class in cocos2d with javaScript

    Posted 6 months ago #
  9. riq
    Key Master

    @banna31:
    Which class/method are you trying to call ?
    Have you looked at the samples bundled with v2.1-beta3?

    Posted 6 months ago #
  10. eurg
    Member

    I’m very impressed by this project, this is a very interesting way of managing multi-platform code base. However I have a few questions :

    1) Could you explain why JS is better for prototype than Obj-C ? I’m not an expert in JS and it’s not obvious for me why writing code in JS is faster/easier than in Obj-C, especially as XCode doesn’t look great for JS.

    2) How do you debug JS code ? Is there a way to have breakpoints and watch variable in an editor or do you have to deal only with log messages ?

    3) Does the JS script files stands next to the binary once the project is compiled ? If so won’t the user be able to simply change the content of these files to alter the game ?

    4) Finally, what would be your recommendation for a new game where iOS is the primary platform? Starting from scratch with Obj-C or JS ?

    Thanks !

    Posted 6 months ago #
  11. Jason A
    Member

    @eurg you can prototype in chrome, which has a debugger & repl etc. there's plenty of good tools for working with js. js is also garbage-collected, unlike objc.

    re #3, yes, tho if you're really concerned about this you could just encrypt them in some way or whatever

    #4: obj-c. you will need to know it to be effective in the long run, imo.

    Posted 6 months ago #
  12. riq
    Key Master

    @eurg:

    1) Could you explain why JS is better for prototype than Obj-C ? I’m not an expert in JS and it’s not obvious for me why writing code in JS is faster/easier than in Obj-C, especially as XCode doesn’t look great for JS.

    It depends on your experience with JS and Objective-C. If you are very efficient with Objective-C and you don't know JS, then perhaps Objective-C is better for you.
    For me, it is faster (much faster) to prototype in JS. I test ideas faster, I develop code faster and I test code is also faster. CocosBuilder + CocosPlayer are great prototyping tools. You can test your JS code in the device (and simulator too) in just a few seconds. No need to recompile + send the binary to the device.

    For the text editor, I recommend using Sublime Text (http://www.sublimetext.com/).

    2) How do you debug JS code ? Is there a way to have breakpoints and watch variable in an editor or do you have to deal only with log messages ?

    We are developing a proper debugger that will let you put breakpoints, inspect variables, step into the code, etc. But it is not ready yet. For the moment I am "debugging" everything with cc.log

    3) Does the JS script files stands next to the binary once the project is compiled ? If so won’t the user be able to simply change the content of these files to alter the game ?

    Yes, the JS files needs to be shipped with the binary. You can encrypt the JS files if you want (feature not ready yet but super easy to implement) or uglify the JS code with any JS "ugly-fier"

    4) Finally, what would be your recommendation for a new game where iOS is the primary platform? Starting from scratch with Obj-C or JS ?

    It depends on your game. The good thing about JS Bindings is that you are not forced to code either ALL your game in JS or nothing. You can develop part of it in JS and part of it in objective-C.
    If you are new with JS, my recommendation is:
    - Start using JS on basic components like Main Menu, HUD, About, Settings in JS. I highly recommend using CocosBuilder for this too. CocosBuilder can be used from Objective-C and JS. So try to develop as much as possible in CocosBuilder.
    - And if you like it then you can use JS for more complex things like game logic.

    Posted 6 months ago #
  13. eurg
    Member

    You can test your JS code in the device (and simulator too) in just a few seconds. No need to recompile + send the binary to the device.

    Right ! A button to reload the script or an auto-reload every time the script is saved can save a lot of time. Also editing scripts and levels directly from the device sounds great.

    We are developing a proper debugger that will let you put breakpoints, inspect variables, step into the code, etc. But it is not ready yet. For the moment I am "debugging" everything with cc.log

    I can't live without breakpoints and variables watchers so this is great news !

    Thanks for this great info, I will definitely try JS bindings once the debugger is here.

    Posted 5 months ago #
  14. jbverschoor
    Member

    I'm kind of confused. What's the difference (3.x) between: "Custom Class" and "JS Controller" ?

    Posted 5 months ago #
  15. riq
    Key Master

    @eurg:
    Resetting the JS VM is possible now (take a look at CocosPlayer ( https://github.com/cocos2d/CocosBuilder/tree/master/CocosPlayer ). So, you could implement a reset/reload button super easy.

    @jbverschoor:

    Custom Class: any subclass... let's say that you want to create an Enemy, then you subclass Sprite and add the "enemy" behavior on the Sprite subclass.

    Controller: You do not subclass Sprite. What you do is to create an "Enemy Controller" class, that controls the behavior of the Enemy. Instead of subclassing, you compose it.

    Posted 5 months ago #
  16. jbverschoor
    Member

    Hi riq, thanks for clearing that, what are the pro/cons or when would you use which?

    I have some troubles connecting to javascript though. I don't really seem to do anything different than the ccdragon example.
    I have a MainMenuScene and a LevelScene. Both have a javascript controller, which I create with the LevelSceneController.prototype.onDidLoadFromCCB etc.

    Now, I also have a Player.ccbi, but no matter what I try (both custom class and js controller), not even the ctor() gets called.

    Posted 5 months ago #
  17. riq
    Key Master

    @jbverschoor:
    Could you post the code to reproduce in http://gist.github.com or here ? Thanks.

    Posted 5 months ago #
  18. jbverschoor
    Member

    @riq, I think I got lost in some save / publish trouble and some latenight coding :-). I got it working.

    I'm using the html version to prototype, as the player sometimes crashes when I make a mistake, plus the fact that the debugger is pretty good in chrome.
    I've configured my webserver to serve out files from Source/js/* first, so I don't need to re-publish every time I make a change.

    Posted 5 months ago #
  19. jbverschoor
    Member

    How would I enable networking like json requests in a cross-platform kind of way?
    Do I need to create bindings to objective-c code myself and seperate glue code for the html version?

    Posted 5 months ago #
  20. riq
    Key Master

    @jbverschoor: Good to know that it is working.
    Yes, for networking you will have to provide your own API (create your own JS bindings).
    We plan to add some basic support in the mid-term future, but if you have it working, send me the patch :)

    Posted 5 months ago #
  21. DanieL
    Member

    @riq: I can protect my JS files with google's closure compiler ? I did it in cocos2d-html5 and I would like to do it on cocos2d-iphone and cocos2d-x too.
    thanks in advance!

    Posted 4 months ago #
  22. riq
    Key Master

    @DanieL:
    I haven't tried that yet, but I guess it should be possible to do it. You have to tell google closure not to obfuscate the cocos2d JS API... BTW, does google closure have that feature ?

    Posted 4 months ago #
  23. DanieL
    Member

    @riq, sorry for delay!

    I don't know about it feature, I will try see more about it.

    Other question, in cocos2d-X I should use https://github.com/zynga/jsbindings to generates my own bindings ? (cocos2d-X)

    thanks in advance!

    Posted 4 months ago #
  24. DanieL
    Member

    I found it for cocos2d-x binding: https://github.com/funkaster/cxx-generator

    Posted 4 months ago #
  25. Yu Chao
    Member

    Hi Riq,

    Js binding is awesome, we should thank you for the effort.

    Now we want to simply call the js function with some args from native code and get the result, is it possible?

    The background is our server is written by nodejs, we want to reuse some common logic codes (I.e. calculate the player's attack and defence value) in server and client side. Further more, we want to make it downloadable by client app.

    Thanks in advance.

    Posted 4 months ago #
  26. riq
    Key Master

    @Yu Chao:
    Yes, you can call it.
    This doc explains how the callbacks work on cocos2d: https://github.com/zynga/jsbindings#calling-js-code-from-native

    And basically you have to call something similar to this code

    // Calling function "update"
    -(void) update:(ccTime)delta
    {
    	if (_jsObj) {
    		JSContext* cx = [[JSBCore sharedInstance] globalContext];
    		JSBool found;
    		JS_HasProperty(cx, _jsObj, "update", &found);
    		if (found == JS_TRUE) {
    			jsval rval, fval;
    			unsigned argc=1;
    			jsval argv[1];
    			argv[0] = DOUBLE_TO_JSVAL(delta);
    
    			JS_GetProperty(cx, _jsObj, "update", &fval);
    			JS_CallFunctionValue(cx, _jsObj, fval, argc, argv, &rval);
    		}
    	}
    }
    Posted 4 months ago #
  27. creativewax
    Member

    hi riq, maybe a daft question but if i want to move forward with this cross platform way of using cocos2d is it better to use the cocos2d-html build and then by using the bindings compile the code into native builds for iOS and Android too ? many thanks in advance, really excited about all of this :)

    Posted 3 months ago #
  28. riq
    Key Master

    @creativewax:

    Pros / cons of starting with cocos2d-html5 first:

    +: Better debugger ( Chrome, Firefox debuggers are great)
    +: Faster iteration (modify a line of code, a test it )
    -: The cocos2d-html5 environment is a superset of the cocos2d+JSB environment, so your game might not run on cocos2d + JSB if you use browser specific functions

    Pros / cons of starting with cocos2d + JSB first:
    +: It is a subset of cocos2d-html5, so your game should work for on cocos2d-html5 without modifications.
    - / +: Fast iteration if you use CocosPlayer / CocosBuilder. By using CocosPlayer you skip Xcode
    -: No proper debugger yet, but we are working on it.

    What you can do is an hybrid approach:
    - Develop on cocos2d-html5 and test your code by the end of day on cocos2d + JSB just to make sure that it works OK.

    Posted 3 months ago #
  29. creativewax
    Member

    ah right so cocos2d + jsb is something different than the html build, i am starting to understand what it is now a bit more,

    so the cocos2d + jsb is programmed in JS though ? and can you still right native code ?

    i am going to get it all downloaded and have a play, thanks for your quick response ;)

    Posted 3 months ago #
  30. riq
    Key Master

    I think the best way to understand what JSB is, is by following these ideas:

    1. You have cocos2d-iphone, a pure Objective-C game engine.
    2. It is fast, since Objective-C is a compiled language
    3. Now you want to add scripting to support to cocos2d-iphone.
    4. So you add a thin layer on top of cocos2d-iphone. The scripting language could be any language: python, Lua, Ruby, or JS.
    5. But we choose JS, not because it is a great language, but because its VM is very fast and it gives your portability to the Web
    6. So, when you do: cc.Sprite.create("grossini.png"), you are creating an objective-c sprite.

    So: JSB (JavaScript Bindings) is that: cocos2d-iphone + JS bindings.

    Additionally, there is another engine called cocos2d-html5 (http://www.cocos2d-html5.org) which has exactly the same JS API as cocos2d-iphone + JSB.
    So you can run your game on the web for free.
    In cocos2d-html5, when you run cc.Sprite.create("grossini.png"), it will create a cocos2d-html5 sprite instead.

    Posted 3 months ago #

RSS feed for this topic

« Previous1…678Next »

Reply »

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.