How to call Javascript function in Java with Cocos2d-JS?

Hi,

I’m using Cocos2d-JS v3.8.1 and want to call a Javascript function in AppActivity.java to update something.

I found this tutorial
http://www.cocos2d-x.org/wiki/Invoking_Android_Java_methods_from_JavaScript

and try to use the function

app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"JavaScript Java bridge!\")");
            }
        });

However, app crash when calling this code.

@pandamicro stated “You shouldn’t directly run SpiderMonkey APIs (via ScriptingCore) in another thread (Runnable is a thread)” in this issue.

The app still crash when calling the code with

Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"JavaScript Java bridge!\")");

So anyone know how to call Javascript function in Java with Cocos2d-JS?

Thank you.

up up up up !!!

Please Help !

@trungdn

You can try using this

Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
    @Override
    public void run() {
        Cocos2dxJavascriptJavaBridge.evalString("javaCallback()");
    }
});

@Zinitter

Hi,

i had tried. But, it’s crashed when run app.

please share folder source code with me.

Thanks !

Hi @Zinitter @trungdn,

Thanks for this solution. It solved my Android problem. Also can you please provide me the solution of calling javascript function from iOS ??

Call js function from iOS

ScriptingCore::getInstance()->evalString("JSFunctionName()");

Call js function with variable from iOS

NSString *JSFunctionName = [NSString stringWithFormat:@"JSFunctionName(%f, %@)", floatValue, stringValue];
    
const char *cString = [JSFunctionName cStringUsingEncoding:NSASCIIStringEncoding];
ScriptingCore::getInstance()->evalString(cString);

Call iOS function from JS

jsb.reflection.callStaticMethod("iOSFunctionFileHeaderName", "iOSFunctionName");

Call iOS function with variable from JS

jsb.reflection.callStaticMethod("iOSFunctionFileHeaderName", "iOSFunctionName:", variable);

evalString() function is not found in my ScriptingCore Class, Please Help

Thanks for the reply @Zinitter . The code you have mentioned is not working with my cocos creator version 1.8.2
It show me error “No member named ‘evalString’ in ‘ScriptingCore’” on this line “ScriptingCore::getInstance()->evalString”

So i tried with “se::ScriptEngine” instead of “ScriptingCore” but it gives me below log of xcode :-

ReferenceError: Can’t find variable: testThisFunction, location: (no filename):1:17

STACK:

global code@(no filename):1:17

callStaticMethod@[native code]

This is my JS function :-

testThisFunction: function(){
console.log(“TEST THIS FUNCTION”);
}

i didnt use cocos creator but with cocos2d-x/js with v3.17 and its working properly, you need to include this header on the iOS file

#include "scripting/js-bindings/manual/ScriptingCore.h"

edited

i just created a cocos creator project and check the files.

i think they rename it to ScriptEngine but the evalString work differently.

#include "cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp"

se::ScriptEngine::getInstance()->evalString();

you need to ask other about the cocos creator as i not familiar how to use it.

this may help, beware the function name is difference

NativeBridge.storeSubscriptionId
NativeBridge.storeRedirectToUser

Hi @Zinitter, You are right. They rename it to ScriptEngine and i have done with the same code you mentioned.
And already gone through the link you have given and it has different solution. It has created new script in resource folder of xcode. But I want to call my predifined script’s function which is in my cocos creator project.
Please share working source code if you have…

Thanks!

sorry, i didnt use cocos creator and has tried some code but not working too.

do you try to call your predefined script function with the code in resource?

@slackmoehrle
do you know who handle about cocos creator issue?

yes i want to call my predifined script function which is in my cocos creator project file, not in resource folder of xcode.