javascriptiosobjective-ccocos2d-iphone

How to pass values to js in cocos2d-iphone jsbindings?


I'm cooking a cocos2d-iphone(v2.1) project with jsbindings. The cocos2d view running main.js in my UIKit view works properly. Now I want to add a new feature to show different animation by user input.

I want to know how to pass values to the js file in objective-c.


Solution

  • I think I found a solution.

    In jsb_core.h, there is a method called: -(BOOL) evalString:(NSString*)string outVal:(jsval*)outVal;. With this method, I can call functions in my main.js from my objective-c code.

    So I defined a function in my main.js file:

    function test(values) {
      cc.log(values);
    }
    

    Then I can call it from my objective-c code like this:

    CCDirectorIOS *director = (CCDirectorIOS *)[CCDirector sharedDirector];
    if(director.runningScene) {
      [[JSBCore sharedInstance] evalString:@"test('ValueToPass');" outVal:nil];
    }