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

    Status:
    Converting Chipmunk JS API into an Object Oriented JS API.

    Instead of:

    cp.space.setGravity( g );

    This:

    space.setGravity( g ;

    The Chipmunk JS API will be 100% compatible with the Chipmunk-JS project. So you will be able to port your physics game to the web for free ( https://github.com/josephg/chipmunk-js )

    Posted 8 months ago #
  2. odyssey
    Member

    @riq: I think jsbindings are great. I wonder how one can reload javascript code when the app is running?
    In that way the development process would be much faster.

    Posted 8 months ago #
  3. riq
    Key Master

    yes, what you can do is to edit the file js_bindings_core.mm, and edit the runScript method.
    You will see that it loads the script from the "bundle". If you change that code, you can tell it to load the script from a custom path, and then add a button in your game that just reloads the script.

    Another missing functionality, is a way to reset the JS VM.

    Posted 8 months ago #
  4. DanieL
    Member

    Is there a way to correct formatting for just *.js files in Xcode? The indentation are wrong in my javascript files. :(

    - XCode 4.4.1
    - cocos2d-iphone v2.1 beta2

    Thanks in advance!

    Posted 8 months ago #
  5. riq
    Key Master

    @DanieL:
    Xcode as a JS editor sucks.

    Try with sublime, or mac vim, or your favorite text editor:
    http://www.sublimetext.com/
    http://code.google.com/p/macvim/

    Posted 8 months ago #
  6. riq
    Key Master

    Update:

    Object Oriented Chipmunk API is automatically generated.
    Status:
    - basic tests are working, but I know it will crash on when the garbage collector runs. I need to root the objects that are added to the space.

    Posted 8 months ago #
  7. DanieL
    Member

    @riq:
    Thank you very much, I liked the sublime ;)

    Posted 8 months ago #
  8. odyssey
    Member

    @riq: How do we reload the script via the button, by executing runScript from javascript ?

    And how does one reset the JS VM?

    Posted 8 months ago #
  9. riq
    Key Master

    @odyssey:
    "reset" the JS VM is not implemented yet. But you can reload an script by calling require('script.js') from js.

    Posted 8 months ago #
  10. DanieL
    Member

    I'm having error when using cc.Rect.CCRectContainsPoint
    TypeError: cc.Rect is undefined

    How should I do? I am following the reference: http://www.cocos2d-x.org/reference/html5-js

    Thanks in advance!

    Posted 8 months ago #
  11. DanieL
    Member

    @riq: I've been thinking about a situation. With the game written in cocos2d + JS, there is a way to work with Game Center? I know I can connect, as this can be done in the delegate, but when I call a script I can send the score? Can I call a function that is in delegate? Another question is about scenes, I can have scenes in cocos2d and others in JS?

    Sorry the questions, but I've read all the material that you did on the cocos2d + JS and I'm really very interested and I'm really wanting to understand how all this will work.

    Thank you!

    Posted 8 months ago #
  12. riq
    Key Master

    @DanieL:

    Yes, to create a rect you should use:

    var r = cc.rect(x,y,w,h);

    And the rect API was not finished, but it should be something like this:

    var bool2 = r.rectContainsPoint( point );

    Could you open a ticket, or better, could you send me a patch with it ? :)
    I am using the "JS Bindings" project to report bugs/issues for the js bindings:
    https://github.com/zynga/jsbindings

    The function should be added to this file:
    https://github.com/zynga/jsbindings/blob/master/src/js/jsb_constants_cocos2d.js#L202

    Posted 8 months ago #
  13. igalic
    Member

    Hi,

    are there any known issues with targeted touch dispatcher? According to HTML5 docs, it should work like this:

    cc.TouchDispatcher.addTargetedDelegate(this, 0, false);

    But that says that cc.TouchDispatcher is undefined.

    I'm coming from native Obj-C and this is my first time dealing with JS, so I may be doing something wrong :)

    Posted 8 months ago #
  14. andersonf78
    Member

    @igalic -- try this:

    onEnter:function () {
    cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 0, true);
    this._super();
    },

    onExit:function () {
    cc.Director.getInstance().getTouchDispatcher().removeDelegate(this);
    this._super();
    },

    Posted 8 months ago #
  15. riq
    Key Master

    @igalic:
    For the moment, the Targeted Touches are not supported on the JS bidnings. But they work OK on cocos2d-html5.

    A good rule of thumb is:
    If the functionality is present on the JS Tests, Moon Warriors or Watermelon with Me (all of them bundled with cocos2d v2.1-beta2) then it should work. Else, most probably it is not supported yet.

    Also, I highly recommend to base your game on the "Watermelon with Me" code. It follows the cocos2d JS Bindings best practices.

    Posted 8 months ago #
  16. igalic
    Member

    Thanks, I've now switched to non-targeted version. I managed to get my prototype running in the browser (originally written in JS for iPhone), I'm amazed at how this all is coming together :)
    I have a few questions about porting and compatibility:

    - how are cc.associateWithNative handled? The html version didn't seem to mind, although their code uses this._super()
    - is there a recommended way to get both versions running in a compatible way? What I did was create a main-iphone.js file where I put the 'run' function, and then have a separate main.js file which extends cc.Application and runs it from there. Main game logic is in a separate third file used by both of those. It's mainly working except that I couldn't get both versions to run the same way, i.e. the html-5 version:

    var ConnectScene = cc.Scene.extend({
        onEnter:function () {
            this.associateWithNative(this, cc.Scene);
            var layer = new MainMenuLayer();
            this.addChild(layer);
        }
    });

    // and then director.runWithScene(new ConnectScene()) in main.js

    The iPhone version gave me an error here:

    jsb: ERROR in jsval_to_nsobject: Error obtaining proxy

    So I had to use the 'run' function from the sample. Not a big deal but I don't really understand the issue.
    I had to add
    cc.Scene.extend = cc.Class.extend;
    to jsb constants file.

    After that I got it working. But for some reason, it's as if the anchor for the html5 version is not in the middle of the canvas - my whole game is centered around lower-left corner, and I haven't been able to find a way to fix that.
    (EDIT: I found the problem eventually - from my main menu I was running replaceScene({a layer}) instead of {the scene} which I prepared. It worked fine on the iPhone but not in html. Now I have the whole prototype working in browser, woohoo! :) )

    In any case, thanks for the great work!

    Posted 7 months ago #
  17. oskiller
    Member

    hi,guys

    I run js moon warriors test on itouch 4, the performance scare me a lot.

    fps :~ 10 FPS ,very unstable

    Did I do something wrong?

    and of cause, thanks for the great work.

    Posted 7 months ago #
  18. riq
    Key Master

    @oskiller:
    A few things to keep in mind:
    - compile it in "release" mode. The "debug" mode is super slow.
    - the game was not tuned for performance. In fact, it doesn't follow many of the best practices.
    - To learn the "js bindings" best practices, please take a look at the "Watermelon With Me" source code.

    On an iPhone 4S on release mode Moon Warriors runs at 40FPS. When the GC is called, the FPS drops a bit.

    Posted 7 months ago #
  19. riq
    Key Master

    @igalic:
    I started to write info about subclassing here:
    https://github.com/cocos2d/cocos2d-iphone/wiki/JS-Bindings-Programming-Guide

    Currently it doesn't explain how to do it with "Class.extend", but basically what you have todo is:

    var AboutLayer = cc.Layer.extend({
        ctor:function () {
           // This is compatible with cocos2d-html5.
           // When using JS-Bindings, it associates the native object with a JS object
           // when using cocos2d-html5, it does nothing
            cc.associateWithNative( this, cc.Layer );
        },
        init:function () {
            if (this._super()) {
                // do whatever you want
            }
            return true;
        },
    });

    The game Moon Warriors ( https://github.com/ricardoquesada/MoonWarriors ) shows how to subclass.

    Posted 7 months ago #
  20. oskiller
    Member

    @riq:

    yes, i got 40 fps now ,thanks for your advice.

    but GC still a problem.

    we will make cocos2d + jsb as our choice for next game if spidermonkey GC issue was resolved.

    Posted 7 months ago #
  21. riq
    Key Master

    @oskiller:
    As I mentioned, "Moon Warriors" does not follow the "js bindings" best practices. It creates unnecessary objects, and it doesn't reuse objects when needed.

    Please, take a look at "Watermelon" code and performance. It runs at 60 FPS on iPad 1 / iPod touch 4, and it has no problems with the GC.

    Posted 7 months ago #
  22. oskiller
    Member

    @riq

    sorry i missunderstand your words. because watermelon was crashed when i debug on device...

    thank you for fix me.

    It's time to deep in JSB programming :)

    Posted 7 months ago #
  23. rsanchezsaez
    Member

    I have one small question: is adding Microsoft Windows and/or Linux layers for Cocos2d Javascript planned for the future? In my mind they are the only big non-console platforms missing (as Mac OS X, iOS and Android are all supported).

    Posted 7 months ago #
  24. riq
    Key Master

    @rsanchezsaez: yes. you can use JS Bindings for Linux/Windows by running them on top of cocos2d-x (and not on top of cocos2d-iphone).
    cocos2d-x for windows is stable. I don't know how stable is for linux (it could be stable or not... it is that I just don't know).

    Posted 7 months ago #
  25. yosit
    Member

    I have a bit of an issue to use ccDrawLine from JavaScript. What is the right syntax for it?

    Thanks for the great work you're doing on this!

    Posted 7 months ago #
  26. riq
    Key Master

    @yosit:
    ccDrawLine is not supported in JavaScript yet.
    As a workaround, use the CCDrawNode class (from v2.1) that it is wrapped in JS.
    Watermelon With me shows how to use it from JS.

    Posted 7 months ago #
  27. noocell
    Member

    @riq

    How is one supposed to handle things like Facebook interaction, IAP, push messages etc within JSB? Is there a way to handle such things in Javascript too?

    Thanks!

    Posted 7 months ago #
  28. Jason A
    Member

    @noocell
    I don't know what the long term plans are for which classes will be provided in JS out of the box, but for now you'd have to wrap the relevant classes yourself with the JSB tools (as you would for any native classes you wrote yourself)

    @riq
    is there a pre built project somewhere setup for testing js code on cocos2d-iphone & cocos2d-html5 at the same time?
    I've set up my own, and getting things working has required writing shims for both projects in a few places. If there's some
    sort of official 'compat' project I'd rather use that and have a single place to send fixes etc.

    Posted 7 months ago #
  29. noocell
    Member

    extending Jason A's desire, it would be also very helpful a template or some directions on how to take a HTML5 project (written in webstorm on whatever OS for convenience) and make it run on JSB for iphone.

    Posted 7 months ago #
  30. DanieL
    Member

    It is possible to work with Game Center within JSB?

    Thank you!

    Posted 7 months ago #

RSS feed for this topic

« Previous1…456…8Next »

Reply »

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.