日期:2014-05-16  浏览次数:20473 次

新人求问!一个小的javascript程序出问题debug不出来
<html>
<head>
<title>Project #4</title>
<script>
var LightId; //Location of Lightening
var picSize=10; //Size of every div
var rMax=500/picSize; //Number of Rows (with picSize for each div)
var cMax=500/picSize; //Number of Columns 
var divMax=rMax*cMax; //Total number for divs
var NWood=divMax*0.8; //Total number of wood
var NWater=divMax*0.15; //Total number of water
var NBrick=divMax*0.05; //Total number of brick
var wood=0,water=0,brick=0;
//setInterval("setFire()",5000); //Interval for lightening
setInterval("spreadFire()",100); //Interval for fire spreading

function generateBg(){ //draw random background
var grid=document.getElementById("board");
for (var r=0;r<rMax;r++) //build div location
for (var c=0;c<cMax;c++){
grid.innerHTML+="<div onclick='Handler(event)' id='"+parseInt((r*rMax)+c)+"' style='position:absolute;background-color:yellow;top:"+r*picSize+"px;left:"+c*picSize+"px;width:"+picSize+"px;height:"+picSize+"px;'></div>";
}

while (wood<NWood){ //set wood
var WoodId=Math.floor((Math.random()*divMax));
if (document.getElementById(WoodId).style.backgroundColor == "yellow"){
document.getElementById(WoodId).style.backgroundColor="green";
wood++;
}
}

while (water<NWater){ //set water
var WaterId=Math.floor((Math.random()*divMax));
if (document.getElementById(WaterId).style.backgroundColor == "yellow"){
document.getElementById(WaterId).style.backgroundColor="blue";
water++;
}
}

while (brick<NBrick){ //set brick
var BrickId=Math.floor((Math.random()*divMax));
if (document.getElementById(BrickId).style.backgroundColor == "yellow"){
document.getElementById(BrickId).style.backgroundColor="red";
brick++;
}
}
setFire();
}

function setFire(){ //set fire

LightId=parseInt(Math.floor(Math.random()*divMax)); //random location for lightening

if (document.getElementById(LightId).style.backgroundColor == "green"){ //if lightening attacks wood

document.getElementById(LightId).style.backgroundColor="white";
setTimeout(function(){document.getElementById(LightId).style.backgroundColor="yellow";},1000);
}
if (document.getElementById(LightId).style.backgroundColor == "yellow"){ //if lightening attacks somewhere burning

document.getElementById(LightId).style.backgroundColor="white";
setTimeout(function(){document.getElementById(LightId).style.backgroundColor="yellow";},1000);
}
if (document.getElementById(LightId).style.backgroundColor == "black"){ //if lightening attacks somewhere burned

document.getElementById(LightId).style.backgroundColor="white";
setTimeout(function(){document.getElementById(LightId).style.backgroundColor="black";},1000); }
if (document.getElementById(LightId).style.backgroundColor == "blue"){ //if lightenin