Call custom JS code from java with Cocos2dxJavascriptJavaBridge

Call custom JS code from java with Cocos2dxJavascriptJavaBridge
0.0 0

#1

Hi,

I’m working with Cocos Creator. In order to call a JS function from java, I have tried this:

Java code:

Cocos2dxJavascriptJavaBridge.evalString(“onTwitterLogin(” + accessToken.getToken() + ", " + accessToken.getTokenSecret() + ", " + accessToken.getUserId() + “)”);

JS code:
function onTwitterLogin(token, tokenSecret, userId)
{
// …
}

but the logcat shows:

cocos js error:: ScriptingCore::evalString line:1 msg:ReferenceError: onTwitterLogin is not defined
cocos2d-x: ScriptingCore::callFunctionName error:__errorHandler wasn’t found!

I don’t know what is wrong in this code. Any help?

Thanks in advance


#2

Please, can help me? I really need a solution. I have found a similar post written one year ago… I am not sure if this functionality is working properly in cocos2dx:

Thanks


#3

I have get the solution. I have to call first require(script) in the string to eval, then I call the methos as:

		Cocos2dxJavascriptJavaBridge.evalString("var LoginScene = require('LoginScene'); LoginScene.prototype.onTwitterLogin(\"" + accessToken.getToken() + "\", \"" + accessToken.getTokenSecret() + "\", \"" + accessToken.getUserId() + "\")");

#4

Can you tell this ‘require(“LoginScene”);’ is your LoginScene.fire or LoginScene.js or something else? I am stuck here, any help will be highly appreciated. Thank You.


#5

The require sentence uses the java script file


#6

Thanks so much !


#7

This helped me a lot… Thanks