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

    Update:
    class: 50%. Class methods are generated. Includes are generated automatically. Improved return values. Script is a bit easier to extend. Added Director, Layer, ScaleBy, and more.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%

    @joekim: Thanks.

    Posted 11 months ago #
  2. riq
    Key Master

    Update:
    class: 55%: Files are generated in in file. Registration code is also generated. Script is easier to extend. What's next: remove proxy code. It will be faster to use subclasses.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%

    Posted 11 months ago #
  3. riq
    Key Master

    Update:
    class: 60%: So, I removed the proxy code and did everything with subclasses. Then I realized that subclasses is not a good option. Rolled back to proxy code.
    methods that returns JSObject, returns, if it exists, the already created JSObject. Easier to add callbacks from ObjC.
    Added configuration file. Easier to run the script.
    Next: Add callbacks.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%

    Posted 11 months ago #
  4. l4u
    Member

    it's exciting to receive updates from this post!

    Posted 11 months ago #
  5. dany
    Member

    true...please continue to update us when you do some progress

    Posted 11 months ago #
  6. riq
    Key Master

    Update:
    class: 70%: Native callbacks like onEnter, onExit are working Ok.
    Next: convert target:selector and/or blocks to JS functions. Needed for MenuItems and more.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%

    Posted 11 months ago #
  7. riq
    Key Master

    Update:
    class: 73%: Arrays as arguments are parsed Ok. Fixed some name collision. Added Sequnce, Texture, ActionManger, TextureCache, Menu, MenuItem (without callback) and other classes.
    Next: Support callback in MenuItem.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%

    Callbacks:
    I have been thinking about the callbacks issue all day, and I think the best way to support them is by using blocks. More info tomorrow.

    Posted 11 months ago #
  8. riq
    Key Master

    Update:
    class: 75%: API that uses blocks (menuItem, e.g.) work Ok. CGSize wrapped. Easier to add non-supported types. Better error reporting.

    // works OK.
    var item1 = cc.MenuItemFont.itemWithStringBlock( "Click Me", function( sender )
    	{
    		cc.log("Clicked me from" + sender );
    	} );

    Next: Start developing JS test cases / demo code, similar to cocos2d-iphoine tests (eg: SpriteTest.m, ActionTest)

    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%
    test case: 0%

    Posted 11 months ago #
  9. riq
    Key Master

    Update:
    class: 80%: Today was a "breakthrough" day. I wanted to port the cocos2d-html5 tests cases to cocos2d-iphone + JS bindings.
    The problem was that cocos2d-html5 is using "extend" to create JS "subclasses".
    After doing some tests with @funkaster, we realized that the the "objective-c" object was not being used by the JS "subclasses", and we needed a way to associate the JS subclass with the objective-c object. After doing some tricks, I managed to have it working.
    So, now it is possible to use JS "subclasses". eg: you can "subclass" CCSprite if needed.
    I will try to port the cocos2d-html5 tests tomorrow.

    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%
    test case: 0%

    Posted 11 months ago #
  10. riq
    Key Master

    Update:
    class: 81%: Little progress today.
    I found out that a "subclass" of a "subclass" of a native class doesn't work. Still trying to figure out how to solve it.
    Fixed "init" method building incorrect class under certain circumstances.

    enums: 0%
    functions: 0%
    macros: 0%
    structures: 0%
    constants: 0%
    test case: 1%
    Started cocos2d-html5 sprite test case port

    Posted 11 months ago #
  11. riq
    Key Master

    quick update:
    class: 82%: subclass of subclass of native class works as expected.

    Posted 11 months ago #
  12. riq
    Key Master

    Update:
    class: 85%:
    I found that multiple "subclassing" might cause problems with the Garbage Collector.
    Added all actions.
    Script supports regular expressions for the classes to parse and classes to ignore.
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 2% CGSize and CGRect work as argument and also as return values
    constants: 0%
    test case: 5%:

    Posted 11 months ago #
  13. jptsetung
    Member

    Oh wait, just another question about JS bindings. Does it mean that I could create an app (a kind of cocos2d appstore?) that can downloads a new game and launch it dynamically (a zip of assets+javascript code) ?

    Posted 11 months ago #
  14. slembcke
    Administrator

    Apple would never allow that. If they caught you doing it they could pull you app and/or your developer agreement.

    Posted 11 months ago #
  15. riq
    Key Master

    Update:
    class: 90%: NSArray,CCArray are supported as return values
    enums: 0%
    functions: 0%
    macros: 0%
    structures: 2%
    constants: 0%
    test case: 5%:
    script: fixed many bugs in "classes_to_bind" and "classes_to_ignore".
    open bugs: a) subclassing of subclassing. b) created objects should be "rooted" in order to make to prevent bugs with the GC.

    I will focus on structures now, and in cocos2d-html5 compatibility.

    Posted 11 months ago #
  16. riq
    Key Master

    Update:
    class: 90%
    enums: 0%
    functions: 5%: Added boilerplate code.
    macros: 0%
    structures: 30%: Works with generic structs as long as they all of them have the same type
    constants: 0%
    test case: 5%:
    script: Code is cleaner. Easier to extend and mantain.

    Posted 11 months ago #
  17. riq
    Key Master

    Update:
    class: 90%
    enums: 0%
    functions: 80%: As complete as "class", but I also want to add "opaque" structures. It will be easier to add support for Chipmunk and other C libraries
    macros: 0%
    structures: 30%: Works with generic structs as long as they all of them have the same type
    constants: 0%
    test case: 5%:
    script: Added "method_properties". Supports rename, ignore, callbacks. More coming soon.

    If you try the current code, it will crash on an assert... which is sort of OK, because I am in the middle of supporting optional arguments.

    Posted 11 months ago #
  18. jptsetung
    Member

    @slembcke if you download a new world for your game with a couple of new ennemies+new AI (that's where the javascirpt bindings can be useful), that should be OK with Apple. But I agree that if you downlad totally new games, Apple mght reject it, despite it's technically the same thing.

    Posted 11 months ago #
  19. noocell
    Member

    Just wanted to say that I'm also very interested on using Javascript for Cocos2d. I have used Unity3d and Corona (lua) until now. The last time I tried to write ObjC for cocos2d I had a difficult time. Scripting languages are much better for casual games development.

    This project not only brings a lot of soft programmers to cocos2d (voila!) but also stands as the perfect solution for a device-independent platform.

    From what I can understand, if I now start learning how to use cocos2d-html5, I am going to be absolutely ready when the Javascript - bindings are going to be completed. Isn't this right? The syntax will be the same and the only difference is that with JS-bindings we will have almost native performance on iOS. Right? (I mainly target iOS)

    Posted 11 months ago #
  20. riq
    Key Master

    @noocell:
    Yes, that's correct. the cocos2d-html5 API and the cocos2d-iphone JS API are going to be the same.

    Posted 11 months ago #
  21. riq
    Key Master

    Update:
    class: 90%
    enums: 0%
    functions: 80%:
    macros: 0%
    structures: 30%:
    constants: 0%
    test case: 5%:
    script: Fixed bugs with non-rooted objects. variadic_2_array and optional_args_since works as expected.

    OLD API:

    var sprite = cc.Sprite.spriteWithFile("grossini.png");
    var sprite2 = cc.Sprite.spriteWithFileRect("grossini_atlas.png",  rect );
    var action = cc.RotateBy.actionWithDurationAngle(2, 360)
    var seq = cc.Sequence.actions( [action1, action2] );
    var menu = cc.Menu.menuWithItems( [item1, item2] );

    NEW API (generated automatically by the script):

    var sprite = cc.Sprite.create("grossini.png");
    var sprite2 = cc.Sprite.create("grossini.png", rect);
    var action = cc.RotateBy.create(2, 360);
    var seq = cc.Sequence.create( action1, action2 );
    var menu = cc.Menu.create( item1, item2 );

    These new API is generated automatically by the script. The configuration file has "convert" rules.
    https://github.com/ricardoquesada/cocos2d-iphone/blob/js-spidermonkey/tools/js/cocos2d_spidermonkey.ini

    I still need to add more rules for the rest of the classes.
    The idea is:
    - If you want to change the output, you have to modify the config file, not the script.

    Bugs:
    - GC crashes if the callback function is not a "property" (still investigating)
    - Needs a native initial scene, otherwise the GC will complain (not a big deal)
    - "Subclasses" with "extend" have issues

    Posted 11 months ago #
  22. riq
    Key Master

    Update:
    class: 90%
    enums: 0%
    functions: 80%:
    macros: 0%
    structures: 35%: Started support for "opaque structures"
    constants: 0%
    test case: 5%:
    script: works with "file only" and "class only" projects.

    Currently working on adding JS support for Chipmunk.

    Posted 11 months ago #
  23. slembcke
    Administrator

    Oh? Awesome. I figured I'd try my hand at Chipmunk integration at some point in the near future, but haven't had time yet.

    Posted 11 months ago #
  24. riq
    Key Master

    Yes, I want to have a "demo game" that showcases cocos2d + chipmunk, everything in JS.

    Chipmunk status: 50%
    - I would say that the "common" functions are parsed:
    https://github.com/ricardoquesada/cocos2d-iphone/blob/js-spidermonkey/tests/SpiderMonkeyTest/js_bindings_chipmunk_functions.mm
    - cpSpace, cpBody and cpShape are "opaque structs", so the only way to use them is with its getters/setters

    What's missing:
    - collision callbacks: I don't know how to solve it... One possible idea is to have a custom callback manager in C that binds C callbacks with JS callbacks
    - Register the bound functions (working on this)
    - example

    Posted 11 months ago #
  25. riq
    Key Master

    Update:
    Function registration is working oK.
    Example of chipmunk functions:
    https://github.com/ricardoquesada/cocos2d-iphone/blob/js-spidermonkey/tests/SpiderMonkeyTest/js_bindings_chipmunk_functions_registration.h

    Working on the callbacks now... not sure yet how to do it correctly.
    Specially, I don't know how to bind a CCSprite with a cpBody.... mmm... perhaps I should implement the ChipmunkSprite in C and bind it to JS.

    Posted 11 months ago #
  26. slembcke
    Administrator

    The general idea is that you would pass the closure object as the data pointer for a callback, and register generic callback functions that just invoke the closures. The trick with collision callbacks is that you only get 1 data pointer for the for collision callbacks. What I've done in the past is just to pass a struct of four closure pointers (one for each callback) as the data pointer and have 4 generic callbacks that are registered for the callbacks.

    Something roughly like this (modified from Objective-Chipmunk to try to be more relevant):

    - (void)addCollisionHandlerFortypeA:(id)a typeB:(id)b
    	begin:(CallbackClosure)begin
    	preSolve:(CallbackClosure)preSolve
    	postSolve:(CallbackClosure)postSolve
    	separate:(CallbackClosure)separate;
    {
    	[self removeCollisionHandlerForTypeA:a andB:b];
    
    	handlerContext handler = {
    		a, b, // I save these so I can find/remove the handler from the _handlers array in removeCollisionHandlerForTypeA:andB:
    		begin,
    		preSolve,
    		postSolve,
    		separate,
    	};
    	NSData *data = [NSData dataWithBytes:&handler length:sizeof(handler)];
    
    	cpSpaceAddCollisionHandler(
    		_space, a, b,
    		(begin ? (cpCollisionBeginFunc)Begin : NULL),
    		(preSolve ? (cpCollisionPreSolveFunc)PreSolve : NULL),
    		(postSolve ? (cpCollisionPostSolveFunc)PostSolve : NULL),
    		(separate ? (cpCollisionSeparateFunc)Separate : NULL),
    		(void *)[data bytes]
    	);
    
    	[_handlers addObject:data];
    }

    The only mildly tricky part was making sure that I didn't leak the handlerContext structs.

    Posted 11 months ago #
  27. slembcke
    Administrator

    Sprites:

    I would just steal my ChipmunkSprite class and wrap that: https://github.com/slembcke/SpacePatrol/blob/master/HMUtils/ChipmunkSprite.m

    Otherwise you would need to be able to expose the low level CCNode transform methods, which would probably be terribly slow from Javascript. Or you would need to schedule an update to sync the sprites, and that's caused a lot of confusion and support issues about how sprites and bodies actually worked together.

    Posted 11 months ago #
  28. riq
    Key Master

    @slembcke:
    Thanks... yes, I will use your chipmunk sprite, and add JS bindings for it.. I don't want to expose the "transform" API to JS... well... it is not that I don't want to expose it, but it could be very slow to do that from JS.

    Posted 11 months ago #
  29. riq
    Key Master

    Update:
    class: 91%: callbacks supports arguments (only 1 for the moment) ( update:(ccTime)delta working
    enums: 0%
    functions: 80%:
    macros: 0%
    structures: 40%: opaque structures seems to be working
    constants: 0%
    test case: 5%:
    script: fixes for callbacks.

    Thu/ Fri: Try to have a a very simple chipmunk + cocos2d sample working.

    Posted 11 months ago #
  30. riq
    Key Master

    Added ChipmunkSprite.
    Missing: callback manager

    Get the Video Widget

    Also missing: cocos2d input events (keyboard, touches, accel, mouse...)

    Source code of the mini demo:
    https://github.com/ricardoquesada/cocos2d-iphone/blob/7eb25d4586f49e16707fb2c733380a25c3ce1964/tests/SpiderMonkeyTest/javascript-spidermonkey/test-chipmunk.js

    Posted 11 months ago #

RSS feed for this topic

« Previous123…8Next »

Reply »

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.