如何从JSON文件创建一个对象,该文件存储在与javaScript中的脚本相同的位置?

时间:2015-09-23 16:49:31

标签: javascript json

我正在尝试让我的应用程序(游戏)从存储在服务器上或webapp文件夹中的.json文件中加载指定不同级别内容的数据。该脚本将这些内容存储在对象' levelcontents'每次启动新级别时都需要完全重新加载。这是加载级别的功能(级别0是主菜单):

    var levelcontents;
    function loadLevel(l)
    {
        //just ignore the next line
        level = l;

        switch (l){
        case 0://here 'levelcontents' should get the attributes and methods specified in level0.json 
        break;
        case 1://here 'levelcontents should get the attributes and methods specified in level1.json
        break;
        }
    }

因此,例如,当调用loadLevel(0)并且之后调用levelcontents.draw()时,应该执行特定于级别0(游戏从level0.json获得)的draw()函数。 我已经尝试了一些东西,但我找不到任何可以上班的东西。如果有人能解释我到底需要做什么,最好只在javaScript中我会非常感激。

按要求提供level0.json的内容(不太相关):

{
"draw": function(){
ctx.clearRect(0,0,960,720);
ctx.fillStyle = "#000000";
},
"load": function(){
if (beginning = 0) {erasePrev};
loadHTML('button','level1Button',"position:absolute; top:100px; left:200px;",'LEVEL I','loadLevel(1)',false,false);
loadHTML('a','howtoplayhref',"position:absolute; top:100px; left:100px;",'how to play',false,false,'howtoplay.html');
},
"clear": function(){
removeHTML(level1Button);
removeHTML(howtoplayhref);
}
}

0 个答案:

没有答案