我想从构造2中设计并导出到html5游戏的脚本访问或编辑游戏变量
导出时会创建 data.js &的 c2runtime.js
data.js 由游戏代码和变量组成。
function getGlobalObject(name) {
var runtime = document.getElementById('c2canvas').c2runtime;
for (var p in runtime) {
if(Object.prototype.hasOwnProperty.call(runtime,p)) {
var prop = runtime[p];
if(prop === undefined) continue;
if(prop === null) continue;
if(typeof prop !== 'object') continue;
if(prop.length === undefined) continue;
for(var i = 0; i < prop.length; i++) {
if(prop[i].parent !== undefined &&
prop[i].data !== undefined &&
prop[i].sheet !== undefined &&
prop[i].name !== undefined) {
// probably the global var array
if(prop[i].name === name) {
// that one!
return prop[i];
}
} else {
// no need to loop if not global var array
break;
}
}
}
}
return null;
}
function setGlobalVar(name,value)
{
var g = getGlobalObject(name);
if(g === null) return;
g.data = value;
}
function getGlobalVar(name)
{
var g = getGlobalObject(name);
if(g === null) return 0;
return g.data;
}
setGlobalVar('myGlobal',4); // here is where you set your global
基于上面的代码,我能够使用下面的代码访问全局变量(感谢Yann at scirra)但无法编辑嵌套的其他值。 比如player.hp或者player.money