H5C3J 学习笔记 part7 AreaDivider KineticJS

今天的部分,只在完成同类分区计算的功能,核心计算办法如下:

 var countTimeIndex = 0;  
 function countDiffArea()  
 {  
   var result = 0;  
   for(var i = 0;i < 10; i++)  
   {  
     for(var j = 0; j < 8;j++)  
     {  
       var rect = getObjOnStageByIndex(i,j);  
       if(findSameAround(i,j,rect.attrs.countryType,0))  
       {  
         result += 1;  
         countTimeIndex += 1;  
       }  
     }  
   }  
   changeAreaCount();  
   return result;  
 }  
 function findSameAround(i,j,type,num)  
 {  
   var rect = getObjOnStageByIndex(i,j);  
   var currentType = rect.attrs.countryType;  
   if(rect.attrs.visited)  
   {  
     return 0;  
   }  
   if(currentType != type)  
   {  
     return 0;  
   }else  
   {  
     num += 1;  
     rect.attrs.visited = true;  
     setTimeout(function(){  
       standUpMyRect(i,j);  
       layer.draw();  
     },countTimeIndex*500);  
   }  
   if(i+1 < 10)  
   {  
     findSameAround(i+1,j,type,num);  
   }  
   if(i-1 >= 0)  
   {  
     findSameAround(i-1,j,type,num);  
   }  
   if(j+1 < 8)  
   {  
     findSameAround(i,j+1,type,num);  
   }  
   if(j-1 >= 0)  
   {  
     findSameAround(i,j-1,type,num);  
   }  
   return num;  
 }  
[![](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-29%252520%2525E4%2525B8%25258B%2525E5%25258D%2525884.22.17.png)](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-29%252520%2525E4%2525B8%25258B%2525E5%25258D%2525884.22.17.png)
完成的例子在此,[Demo](http://hector.ziki.me/kinetic/areadivider/AreaDivider.html)