2013-01-23 29 views
0

我正在JavaScript中编写这个非常奇妙的游戏,但是每次运行时我只写了一个函数的一部分崩溃。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; 
    } 
} 

没有if (zloop=1)部分它运行良好。

+0

当然是可以简化.. – 2013-01-23 22:17:27

+0

它的JavaScript告诉你缩进你的代码;) – PeeHaa

回答

4

你的意思是说if (zloop==1)

+0

我刚刚打破了我的鼻子....从facepalming自己很难;我很可怜,哈哈哈,非常感谢你! –

3

你的错误很可能是这一行:

if (zloop = 1) { 

这台zloop1。你想比较zloop1,所以使用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; 
    } 
    } 
} 
+0

谢谢!:) :) :) :) :) :) –

1
if (zloop=1) 

您的意思是

if (zloop == 1) 

=是赋值 ==是比较