当前位置:编程学习 > JAVA >>

Cocos2d-x的SprideMonkey的JavaScript与C++的交互 - 单个全局变量操作

js全局变量的操作.

1.修改值.

该例子是说.js里面有个全局变量x.现在把x获取到c++中. 这块是个字符串.


[cpp]  jsval x; 
JS_GetProperty(sc->getGlobalContext(), sc->getGlobalObject(), "x", &x); 
JSString* pS = JS_ValueToString(ScriptingCore::getInstance()->getGlobalContext(), x); 
JSStringWrapper pW(pS); 
CCLog(pW.get().c_str()); 

            jsval x;
            JS_GetProperty(sc->getGlobalContext(), sc->getGlobalObject(), "x", &x);
            JSString* pS = JS_ValueToString(ScriptingCore::getInstance()->getGlobalContext(), x);
            JSStringWrapper pW(pS);
            CCLog(pW.get().c_str());


2.获取值.

改例子是说.js里面有个全局变量x.现在通过c++把值给设置成HelloWorld!


[cpp]  jsval x = c_string_to_jsval(sc->getGlobalContext(), "HelloWorld!"); 
JS_SetProperty(sc->getGlobalContext(), sc->getGlobalObject(), "x", &x); 

            jsval x = c_string_to_jsval(sc->getGlobalContext(), "HelloWorld!");
            JS_SetProperty(sc->getGlobalContext(), sc->getGlobalObject(), "x", &x);
待续...复杂的Arry和Object.

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,