[SOLVED] Cocos2dxJavascriptJavaBridge

ScriptingCore::evaluateScript fail

It’s done !

Thanks !

It’s done !

Thanks !

Cocos2d-js :

1. With jsbc.js file :

var jsbc = {
	
	log : function(){
		cc.log("....jsbc...jsbc....");
	}
	
}

var jsBridge = {
	
	LLog : function(){
		cc.log("....LLog...LLog....");
	}
}

2. Edit in project.json :

"jsList" : [
    "src/resource.js",
    "src/app.js",
    "src/jsbc.js"
]

3. Must compile first : cocos compile -p android

4. Open Eclipse and write :

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                //we must use runOnGLThread here
                sContext.runOnGLThread(new Runnable() {
                    @Override
                    public void run() {
                        	Cocos2dxJavascriptJavaBridge.evalString("jsbc.log()");
                        	Cocos2dxJavascriptJavaBridge.evalString("jsBridge.LLog()");                   	
                    }
                });
            }
        });

5. Run app Android

6. Use ddms to show console log

I wil test on CocosCreator later.

1 Like

Cocos Creator

  1. cc.log() can not excute :

    i had tried show log on ddms, but nothing appeared.

    i had to view ddms many time. and stucked in there someday =.=!!!

2… can not use “this” inside function of comopent

This is component HelloWorld.js of Canvas node.

var TXT = null;

cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },

        text: 'Hello, World!'
    },

    onLoad: function () {
        this.label.string = this.text;
        
        TXT = this.label;
    },

    log : function(){
        cc.log("...xxx...xxxx....");
        console.log("...hello...world....");  
        
        //this.label.string = "...hello...world....";  ==> it will be failed when compile  
        TXT.string = "...hello...world....";
    }
});

This is bundle.project in path_to_project/library/bundle.project
When after built project, that file will be modified.

require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"HelloWorld":[function(require,module,exports){
"use strict";
cc._RFpush(module, '280c3rsZJJKnZ9RqbALVwtK', 'HelloWorld');
// Script\HelloWorld.js

var TXT = null;

cc.Class({
    "extends": cc.Component,

    properties: {
        label: {
            "default": null,
            type: cc.Label
        },

        text: 'Hello, World!'
    },

    onLoad: function onLoad() {
        this.label.string = this.text;

        TXT = this.label;
    },

    log: function log() {
        cc.log("...xxx...xxxx....");
        console.log("...hello...world....");

        //this.label.string = "...hello...world...."; ==> failed
        TXT.string = "...hello...world....";
    }
});

cc._RFpop();
},{}]},{},["HelloWorld"])

This is Java code :

//we must use runOnUiThread here
app.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        AlertDialog alertDialog = new AlertDialog.Builder(app).create();
        alertDialog.setTitle("title");
        alertDialog.setMessage("message");
        alertDialog.setIcon(R.drawable.icon);
        
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	app.runOnGLThread(new Runnable() {
                    @Override
                    public void run() {
                    	
                    	Cocos2dxJavascriptJavaBridge.evalString("var hello = require('HelloWorld'); hello.prototype.log();");
                    	
                    }
                });
            }
        });
        
        alertDialog.show();
    }
});
1 Like

@trungdn Thanks, this post really helped me.