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

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

#1

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.


#2

up up up up !!!

Please Help !


#3

@trungdn

You can try using this

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

How to call a javascript function from a file gamescene.js inside AppActivity.java
#4

@Zinitter

Hi,

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

please share folder source code with me.

Thanks !