JS 連鎖泡泡 v1.1_javascript技巧

來源:互聯網
上載者:User
泡泡的移動,碰撞,消失都完成了,越寫越覺得沒意思,後面的關於遊戲性的東西(比如遊戲有多少關,打破多少泡泡算過關)就懶得寫了。有興趣的朋友可以讀一下,因為很多地方用到閉包修正setInterval(這個函數比較賤)。物件導向,注釋比較完整。


<html> <head> <title>原創JS連鎖泡泡 v1.1</title> <style> body{ margin: 0px; padding: 0px; background-color: #000; } #main{ border: solid red 0px; width: 800px; height: 600px; font-size: 48px; position: absolute; background-color: #fff; } .little{ height: 15px; width: 15px; font-size: 1px; border: solid #ccc 0px; position: absolute; left: 100px; top: 100px; background: url(/upload/20090928170442418.gif) no-repeat; filter: alpha(opacity=30); } .big{ height: 61px; width: 61px; font-size: 9pt; border: solid #ccc 0px; position: absolute; left: 0px; top: 0px; background: url(/upload/20090928170442175.gif) no-repeat; /**使顯示的分數置中**/ line-height: 61px; color: #fff; filter: alpha(opacity=30); } #info{ position: absolute; left: 820px; top 20px; border: solid #fff 1px; height: 600px; width: 300px; color: #fff; padding: 5px; } #score{ float: right; border: solid #ccc 1px; height: 10px; width: 80px; margin: 5px; font-size: 30px; } </style> </head> <body> <div id="main"> <div id="score">0</div> </div> <div id="info"> 原創JS連鎖泡泡 v1.1 原創作者 sunxing007 轉載請務必註明來自: http://blog.csdn.net/sunxing007 開發環境:IE8 暫時沒有考慮相容性 </div> </body> </html> <script type="text/javascript"> /******************************************************* * 原創JS連鎖泡泡 v1.1 * 原創作者 sunxing007 * 轉載請務必註明來自:http://blog.csdn.net/sunxing007 *******************************************************/ var doc = document; function $(id){return doc.getElementById(id)}; var container = $('main'); var score_ele = $('score'); try{ //針對ie6緩衝背景圖 document.execCommand("BackgroundImageCache", false, true); }catch(e){alert(e)}; /*******************************************************/ //系統參數 //小泡泡半徑 var little_radius = 7; //爆炸後的泡泡半徑 var big_radius = 30; //半徑差 var delta_radius = big_radius - little_radius; //容器寬度 var container_width = 800; //容器高度 var container_height = 600; //與定義的泡泡移動的4個方向 //[x,y]代表x方向的增量和y方向的增量,也就確定了移動方向 var direct = [[1,1],[1,-1],[-1,1],[-1,-1]]; //爆炸後的泡泡集合 var chained_bubbles = []; //所有的泡泡 方便管理 var all_bubbles = []; /*****************************************************/ //泡泡模型 function Bubble(left, top, bgPos, score, dirIndex){ var ele = doc.createElement('div'); ele.align = 'center'; ele.className = 'little'; ele.style.left = left + 'px'; ele.style.top = top + 'px'; this.bgPos = bgPos; ele.style.backgroundPositionX = -bgPos*(little_radius*2+1); this.element = ele; ele.bubble = this; this.score = score; this.exploded = false; this.dirIndex = dirIndex; this.container.appendChild(ele); this.container.onclick = function(){ if(window.curBubble){ //curBubble.distroy.apply(curBubble); chained_bubbles.push(curBubble); curBubble.explode.apply(curBubble); curBubble.container.onclick = null; } } ele.onmouseover = function(){ this.style.filter = 'alpha(opacity=100)'; window.curBubble = ele.bubble; } ele.onmouseout = function(){ this.style.filter = 'alpha(opacity=30)'; } } Bubble.prototype = { //容器的引用 container: $('main'), //移動泡泡 move: function(){ if(chained_bubbles.length>0){ for(var i=0; i<chained_bubbles.length; i++){ if(this.crushed(chained_bubbles[i])){ this.explode(); } } } var _self = this; //連續移動泡泡的函數,因為要用到setInterval, //所以這個函數作成閉包 var _f = function(){ if(chained_bubbles.length>0){ for(var i=0; i<chained_bubbles.length; i++){ if(this.crushed(chained_bubbles[i])){ this.explode(); break; } } } var l = parseInt(this.element.style.left) + direct[this.dirIndex][0]; var t = parseInt(this.element.style.top) + direct[this.dirIndex][1]; //status = 'l:' + l + " t:" + t; if(this.checkBorder(l, t)){ this.element.style.left = l; this.element.style.top = t; } else{ //this.stop(); this.changeDirect(); } } this.timer = setInterval(function(){ _f.apply(_self); //移動速度 }, 10) }, //停止移動 stop: function(){ clearInterval(this.timer); }, //邊界檢查 看泡泡有沒有超出容器 checkBorder: function(l, t){ if(l>(container_width-big_radius-1-little_radius)||l<(big_radius-little_radius)||t>(container_height-big_radius-1-little_radius)||t<(big_radius-little_radius)){ return false; } return true; }, //碰壁後改變泡泡的移動方向 changeDirect: function(){ var _l = parseInt(this.element.style.left); var _t = parseInt(this.element.style.top); for(var i=1; i<=4; i++){ if(direct[this.dirIndex][0]==-direct[(i+this.dirIndex)%4][0]&&direct[this.dirIndex][1]==-direct[(i+this.dirIndex)%4][1]){ continue; } var l = _l + direct[(i+this.dirIndex)%4][0]; var t = _t + direct[(i+this.dirIndex)%4][1]; if(this.checkBorder(l, t)){ this.dirIndex = (i+this.dirIndex)%4; break; } else{ continue; } } }, //泡泡爆炸 explode: function(){ this.stop(); this.element.style.left = parseInt(this.element.style.left) - delta_radius; this.element.style.top = parseInt(this.element.style.top) - delta_radius; this.element.className = 'big'; this.element.style.backgroundPositionX = - this.bgPos*(big_radius*2+1); this.element.innerText = this.score; this.exploded = true; score_ele.innerText = parseInt(score_ele.innerText) + this.score; chained_bubbles.push(this); this.fade.apply(this); }, /**是否和另外一個已經爆炸的泡泡碰撞了**/ crushed: function(other){ if(!other.exploded){ return false; } if(!other.element) return false; var l = parseInt(other.element.style.left); var t = parseInt(other.element.style.top); var w = big_radius*2-8;//big_radius*2+1; var h = w; var pt = [,,,]; pt[0] = [parseInt(this.element.style.left) - delta_radius, parseInt(this.element.style.top) - delta_radius]; pt[1] = [pt[0][0]+w,pt[0][1]]; pt[2] = [pt[0][0]+w,pt[0][1]+h]; pt[3] = [pt[0][0],pt[0][1]+h]; for(var i=0; i<4; i++){ if(pt[i][0]>l&&pt[i][1]>t&&pt[i][0]<=(l+w)&&pt[i][1]<=(t+h)){ return true; } } return false; }, //爆炸的泡泡逐漸消失 fade: function(){ var _self = this; var opacity = 28; var _f = function(){ this.element.style.filter = 'alpha(opacity=' + (opacity-2) + ')'; opacity -=2; if(opacity==0){ this.distroy(); } }; this.timer = setInterval(function(){ _f.apply(_self); }, 200); }, //析構方法 因為對象中很多地方涉及對象相互引用 //需要在解構函式中消除環形引用,以防記憶體泄露 distroy: function(){ if(this.timer){ clearInterval(this.timer); } for(var j=0; j<chained_bubbles.length; j++){ //if(chained_bubbles[j]==null) continue; if(chained_bubbles[j]===this){ chained_bubbles.splice(j, 1); //chained_bubbles[j] = null; status = 'distroyed from chained_bubbles[' + j + ']'; break; } } for(var i=0; i<all_bubbles.length; i++){ if(!all_bubbles[i].element){ all_bubbles.splice(i, 1); } if(all_bubbles[i]===this){ all_bubbles.splice(i, 1); status = 'distroyed from all_bubbles[' + i + ']'; break; } } var _e = this.element; this.element = null; _e.onclick = null; _e.onmouseover = null; _e.onmouseout = null; _e.bubble = null; var _c = this.container; this.container = null; _c.removeChild(_e); } } //Factory 方法 創造泡泡 function createBubble(){ var left = Math.floor(Math.random()*710) + 31; var top = Math.floor(Math.random()*510) + 31; var bgIndex = Math.floor(Math.random()*6); var score = Math.floor(Math.random()*10) * 50; var dirIndex = Math.floor(Math.random()*4) var b = new Bubble(left, top, bgIndex, score, dirIndex); all_bubbles.push[b]; return b; } createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); createBubble().move(); /******************************************************* * 原創JS連鎖泡泡 v1.1 * 原創作者 sunxing007 * 轉載請務必註明來自:http://blog.csdn.net/sunxing007 *******************************************************/ </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

轉載請務必註明來自:http://blog.csdn.net/sunxing007

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.