当前位置:编程学习 > JAVA >>

javascript:9宫格拖拽拼图游戏puzzle

用一张图片用坐标的方式生成拼图单元格,类似css sprite。大致思路是,每个单元格有对应的两个索引,由数据驱动显示。创建实例时吧0-9乱序排列,每个单元格对应两个索引,一个是真实所在位置的索引,一个是记录现在位置的索引。拖动图片的时调整记录现在位置的索引,去对比原始数组如果值相等,那么就可以确定拼图完成。
1.乱序排列[0-9]的数组生成一个新数组,来产生拼图单元格的排序。为了避免生成不符合需求的数组,比较然后递归。
Javascritpt代码    
1. indicator.fn.unordered = function (array) {  //乱序排列  
2.       array=array || [0,1,2,3,4,5,6,7,8,9];  
3.       if (({}).toString.call(array).indexOf('Array') === -1) return;  
4.       var arr = [], value = array.toString();  
5.       arr = array.sort(function () {  
6.          return Math.random() > 0.5 ? -1 : 1;  
7.      });  
8.      (arr.toString() === value) && arguments.callee(array);  
9.      return arr;  
10. }; 

 

2.主要难点在于拖拽时实时去检测现在单元格与其他单元格的位置关系,判断拖动的单元格是不是进入其他单元格而不是在自己的占位单元格。如果不符合要求,则把拖拽元素放回原始位置。
Javascritpt代码    
1. indicator.fn.checkPosition=function(e){ //检查单元格位置,符合条件插入 不符合条件放回原处  
2.      e=that.getEvent(e);  
3.      var i=that.htmls.length,size=null,pointer = [e.clientX,e.clientY];  
4.      for(;i--;){  
5.     size=that.getClinetRect(that.htmls[i]);  
6.     if(size.top < pointer[1] && size.bottom>pointer[1]){  
7.         if(size.left<pointer[0] && size.right>pointer[0]){  
8.         that.htmls[i]===indicator.currentTarget || that.replaceElement(that.htmls[i]);  
9.         }else{  
10.          that.restore();  
11.         }  
12.     }  
13.     }  
14. }; 
  
3.为了避免拖拽元素时导致单元格产生的重新排列导致的错位,所以要产生一个占位单元格,这占位的单元格克隆自拖拽单元格。
Javascritpt代码    
1. indicator.fn.createPlaceholder=function(T){  
2.    var node=T.cloneNode(true);  
3.    node.innerHTML='';  
4.    node.style.display='none';  
5.    node.className='placeholder';  
6.    T.parentNode.insertBefore(node,T);  
7.    that.placeholder=node;  
8. }; 

4.界定拖拽单元格是否进入其他单元格,左顶点大于当前单元格的top,left但是小于当前单元格的bottom,right
Javascritpt代码    
1. indicator.fn.checkPosition=function(e){ //检查单元格位置,符合条件插入 不符合条件放回原处  
2.     e=that.getEvent(e);  
3.     var i=that.htmls.length,size=null,pointer = [e.clientX,e.clientY];  
4.     for(;i--;){  
5.           size=that.getClinetRect(that.htmls[i]);  
6.           if(size.top < pointer[1] && size.bottom>pointer[1]){  
7.                 if(size.left<pointer[0] && size.right>pointer[0]){  
8.                    that.htmls[i]===indicator.currentTarget || that.replaceElement(that.htmls[i]);  
9.                }else{  
10.                      that.restore();  
11.                }  
12.          }  
13.     }  
14. }; 


5.调整单元格时,要改变拖拽单元个的两个记录位置的属性,还要改变当前单元格的两个位置属性,再调整记录位置的数组
这里的处理我觉得也是最繁复最有挑战的地方,修改了很久
Javascritpt代码    
1.    indicator.fn.replaceElement=function(targetNode){ //摆放单元至正确位置  
2.  
3. var current=indicator.currentTarget,k1=that.htmls,k2=that.sn,temp,j=0,k=0,a1=[-1,-1],a2=null,a3=-1,arr=[],parent=that.parent.children[0],li;  
4.  
5. current.style.position='static';  
6.  
7. that.index=[];  
8.  
9. for(;k<k1.length;k++){  
10.     if(targetNode===that.htmls[k]) {a1[0]=k;}  
11.     if(current===that.htmls[k]) {a1[1]=k;}  
12. }  
13. a1.sort();  
14.  
15. a2=k1[a1[0]];  
16. k1[a1[0]]=k1[a1[1]];  
17. k1[a1[1]]=a2;  
18.  
19. a3=k2[a1[0]];  
20. k2[a1[0]]=k2[a1[1]];  
21. k2[a1[1]]=a3;  
22.  
23. parent.innerHTML='';  
24.  
25. for(;j<k1.length;j++){  
26.     li=that.htmls[j];  
27.     that.index.push(that.htmls[j].index);  
28.     try{parent.appendChild(li)}catch(e){}  
29. }  

6.在释放鼠标时,检查原始数组和记录位置数组的值十分相等,确定是不是拼图完成。
Javascritpt代码    
1. that.index.join('')===that.sn.sort().join('') 

 

摘自 BlueScript
补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,