Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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 - Fatal编程技术网

JavaScript函数每次运行';快跑

JavaScript函数每次运行';快跑,javascript,Javascript,我正在用JavaScript编写这个非常棒的游戏,但我刚刚编写的函数的一部分在每次运行时都会崩溃 所以我想知道,是在for循环中运行太多,还是我在某个地方犯了语法错误 这是代码;它取自一个函数,但这是使它崩溃的部分 for (zloop=min_houses_per_block; zloop<(house_number+1); zloop++) { if (zloop>0) { city_block_array[first][second].house

我正在用JavaScript编写这个非常棒的游戏,但我刚刚编写的函数的一部分在每次运行时都会崩溃

所以我想知道,是在for循环中运行太多,还是我在某个地方犯了语法错误

这是代码;它取自一个函数,但这是使它崩溃的部分

for (zloop=min_houses_per_block; zloop<(house_number+1); zloop++)
{
    if (zloop>0)
    {
        city_block_array[first][second].house_array[zloop].width =
                (min_house_width+(((max_house_width-min_house_width)/house_width_slots)*(Math.floor(Math.random()*house_width_slots))));

        city_block_array[first][second].house_array[zloop].height =
                (min_house_height+(((max_house_height-min_house_height)/house_height_slots)*(Math.floor(Math.random()*house_height_slots))));

        if (zloop=1)
        {
            x_number=(block_width-(house_threshold*2))-city_block_array[first][second].house_array[zloop].width;
            min_house_x=house_threshold
            city_block_array[first][second].house_array[zloop].x =
                    (min_house_x+(((x_number)/house_x_slots)*(Math.floor(Math.random()*house_x_slots))));
        }

        city_block_array[first][second].house_array[zloop].x=6000;
        city_block_array[0][0].house_array[1].x=6000;
    }
}
for(zloop=min\u houses\u per\u block;zloop 0)
{
城市块数组[first][second]。房屋数组[zloop]。宽度=
(最小房屋宽度+((最大房屋宽度-最小房屋宽度)/房屋宽度插槽)*(数学地板(数学随机()*房屋宽度插槽));
城市块数组[first][second]。房屋数组[zloop]。高度=
(最小房屋高度+((最大房屋高度-最小房屋高度)/房屋高度插槽)*(数学楼层(数学随机()*房屋高度插槽));
if(zloop=1)
{
x_编号=(区块宽度-(房屋阈值*2))-城市区块数组[first][second]。房屋数组[zloop]。宽度;
min_house_x=house_阈值
城市\街区\数组[first][second]。房屋\数组[zloop].x=
(min_house_x+((x_编号)/house_x_插槽)*(Math.floor(Math.random()*house_x_插槽)));
}
城市块数组[first][second]。房屋数组[zloop]。x=6000;
城市\街区\数组[0][0]。住宅\数组[1]。x=6000;
}
}

如果没有
if(zloop=1)
部分,它运行正常。

您的意思是说
if(zloop==1)
您的错误很可能是这一行:

if (zloop = 1) {
这将
zloop
设置为
1
。您想将
zloop
1
进行比较,所以使用
zloop==1

另外,尽量减少线的长度。您可以使用一些更具描述性的变量:

for (var zloop = min_houses_per_block; zloop < (house_number + 1); zloop++) {
  var house = city_block_array[first][second].house_array[zloop];

  if (zloop > 0) {
    house.width = (min_house_width + (((max_house_width - min_house_width) / house_width_slots) * (Math.floor(Math.random() * house_width_slots))));
    house.height = (min_house_height + (((max_house_height - min_house_height) / house_height_slots) * (Math.floor(Math.random() * house_height_slots))));

    if (zloop == 1) {
      x_number = (block_width - (house_threshold * 2)) - house.width;
      house.x = (house_threshold + (((x_number) / house_x_slots) * (Math.floor(Math.random() * house_x_slots))));
    } else {
      city_block_array[0][0].house_array[1].x = 6000;
      house.x = 6000;
    }
  }
}
for(var zloop=min\u houses\u per\u block;zloop<(house\u number+1);zloop++){
var house=city_block_array[first][second]。house_array[zloop];
如果(zloop>0){
house.width=(min_house_width+((max_house_width-min_house_width)/house_width_插槽)*(Math.floor(Math.random()*house_width_插槽));
house.height=(min_house_height+((max_house_height-min_house_height)/house_height_插槽)*(Math.floor(Math.random()*house_height_插槽)));
if(zloop==1){
x_编号=(块宽度-(房屋宽度阈值*2))-房屋宽度;
house.x=(house_阈值+((x_编号)/house_x_插槽)*(Math.floor(Math.random()*house_x_插槽)));
}否则{
城市\街区\数组[0][0]。住宅\数组[1]。x=6000;
房屋x=6000;
}
}
}
你是说

if (zloop == 1)
=这是一项作业
==是一种比较

当然可以简化..它是JavaScript告诉您缩进代码;)我刚打破了我的鼻子。。。。从把脸捂得那么紧;我真可怜,哈哈哈,太谢谢你了!
if (zloop == 1)