Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用函数的参数来使用对象的数据?_Javascript_Arrays_Function_Object_Ecmascript 6 - Fatal编程技术网

Javascript 如何使用函数的参数来使用对象的数据?

Javascript 如何使用函数的参数来使用对象的数据?,javascript,arrays,function,object,ecmascript-6,Javascript,Arrays,Function,Object,Ecmascript 6,我正在尝试创建一个游戏,我想在其中加载一个关卡。我正试图以一种简化的方式来设置它 我已经尝试使用: function loadLevel(name) { return fetch(`./levels/${name}.json`).then(r => r.json());. 这不起作用,因为它甚至不会加载我保存在另一个文件中的级别。我使用console.log对此进行了测试。这是我的json,保存在一个名为1-1的文件中: { "backgrounds": [ {

我正在尝试创建一个游戏,我想在其中加载一个关卡。我正试图以一种简化的方式来设置它

我已经尝试使用:

function loadLevel(name) { return fetch(`./levels/${name}.json`).then(r => r.json());. 
这不起作用,因为它甚至不会加载我保存在另一个文件中的级别。我使用console.log对此进行了测试。这是我的json,保存在一个名为1-1的文件中:

{
  "backgrounds": [
    {
      "tile": "sky",
      "ranges": [
        [
          0, 25,
          0, 14
        ]
      ]
    }
  ]
}
我认为拥有一个设置我的级别的对象可以实现以下目的:

var levels = {
    "1-1": {
        backgrounds: [
            {
                tile: "sky",
                ranges: [[0, 25, 0, 16]]
            }
        ]
    }
}
上面是我需要如何获得每个变量,然后我需要最终通过如下方式传递它:

function drawBackground(backgrounds, ctx, sprites) {
    backgrounds.ranges.forEach(([x1, x2, y1, y2]) => {
        for (let x = x1; x < x2; x++) {
            for(let y = y1; y < y2; y++) {
                sprites.drawTile(background.tile, ctx, x, y);
            }
        }
    });
}
现在我已经列出了所有相关的代码,这就是我的问题:

我想编辑loadLevel函数,以便可以使用对象选择要加载的级别,但不确定具体如何执行。我的意思是类似于fetch()方法。另一种可能的解决方案是通过traffackground()函数运行它

//maybe like this:
function drawBackground(name, ctx, sprites) {
//Below, I know ${name} will not work but an idea of a possible solution
  levels.${name}.backgrounds.ranges.forEach(([x1, x2, y1, y2]) => { 
    for (let x = x1; x < x2; x++) {
      for(let y = y1; y < y2; y++) {
        sprites.drawTile(background.tile, ctx, x, y);
      }
    }
  });
}
//可能是这样的:
功能牵引地面(名称、ctx、精灵){
//下面,我知道${name}将不起作用,但我想到了一个可能的解决方案
levels.${name}.backgrounds.ranges.forEach([x1,x2,y1,y2])=>{
对于(设x=x1;x
我需要做的就是加载关卡和它的属性来绘制它。如果您有任何问题,如果需要,我可以在我的问题中提供更多帮助,无论是完整/更多代码还是其他答案/解释


编辑:通过访问对象的方法解决此问题的可能方法可在此处的另一个问题中找到:

使用方括号表示法:

function drawBackground(name, ctx, sprites) {
    levels[name].backgrounds.ranges.forEach(...);
}
function drawBackground(name, ctx, sprites) {
    levels[name].backgrounds.ranges.forEach(...);
}