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
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
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 Likes
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.
1 Like
The require sentence uses the java script file
1 Like
This helped me a lot… Thanks
hello, jalfonsosm
you can send file LoginScene. or example…
my file js:
var JavaCall = cc.Class({
statics: {
callFromJava() {
// do thing
}
},
});
module.export = JavaCall;
I call from Java:
Cocos2dxJavascriptJavaBridge.evalString(“require(‘JavaCall’).callFromJava();”);
or Cocos2dxJavascriptJavaBridge.evalString(“var JavaCall = require(‘JavaCall’); JavaCall.prototype.callFromJava();”);
not work with me
Thank so much