问题解决了,对executeFunctionWithOwner写了一个包装接口接收一个string参数,然后通过jni导出C++接口给Android用
ScriptingCore.h添加声明
bool callCocosWithString(const char *string);
ScriptingCore.cpp添加实现
bool ScriptingCore::callCocosWithString(const char *string)
{
jsval jsv = c_string_to_jsval(this->getGlobalContext(),string);
this->executeFunctionWithOwner(OBJECT_TO_JSVAL(this->getGlobalObject()),要调用的函数名, 1, &jsv);
}
CCJavascriptJavaBridge.cpp导出接口
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_callCocos
(JNIEnv *env, jclass cls, jstring value)
{
bool strFlag = false;
std::string strValue = cocos2d::StringUtils::getStringUTFCharsJNI(env, value, &strFlag);
if (!strFlag)
{
CCLOG(“Cocos2dxJavascriptJavaBridge_evalString error, invalid string code”);
return 0;
}
ScriptingCore::getInstance()->callCocosWithString(strValue.c_str());
return 1;
}