用仿ActionScript的文法來編寫html5——第七篇,自訂按鈕

來源:互聯網
上載者:User

標籤:

本文為 兄弟連IT教育 機構官方 HTML5培訓 教程,主要介紹:用仿ActionScript的文法來編寫html5——第七篇,自訂按鈕

第七篇,自訂按鈕


這次弄個簡單點的,自訂按鈕。
其實,有了前面所定義的LSprite,LBitmap等類,定義按鈕就很方便了。
下面是添加按鈕的代碼,

[javascript] view plain copy 
  1. function gameInit(event){  
  2.     backLayer = new LSprite();  
  3.     addChild(backLayer);  
  4.       
  5.     btn01 = new LButton(new LBitmap(new LBitmapData(imglist["replay_button_up"])),new LBitmap(new LBitmapData(imglist["replay_button_over"])));  
  6.     btn01.x = 76;  
  7.     btn01.y = 50;  
  8.     backLayer.addChild(btn01);  
  9.       
  10.     btn02 = new LButton(new LBitmap(new LBitmapData(imglist["quit_button_up"])),new LBitmap(new LBitmapData(imglist["quit_button_over"])));  
  11.     btn02.x = 76;  
  12.     btn02.y = 100;  
  13.     backLayer.addChild(btn02);  
  14.       
  15.     btn01.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown01);  
  16.     btn02.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown02);  
  17. }  
  18. function onmousedown01(event){  
  19.     alert("btn01 on click");  
  20. }  
  21. function onmousedown02(event){  
  22.     alert("btn02 on click");  
  23. }  



原理:建立一個繼承自LSprite的LButton類,給按鈕設定兩個圖片,然後偵聽滑鼠位置,當滑鼠移動到按鈕上的時候,變換按鈕狀態,就是一個簡單的按鈕。


這裡,我用mousemove來偵聽滑鼠位置,給LGlobal類添加一個buttonList數組,當建立按鈕的時候,把按鈕加入到buttonList,然後當移動滑鼠的時候,就可以從buttonList數組判斷滑鼠是否在按鈕上,然後當按鈕被刪除後,將按鈕從buttonList數組中刪除。


一些修改:
1,修改LSprite類,添加die方法,每個LSprite當被removeChild的時候,調用自己的die方法,die方法裡放一些被移除是必需處理的事件,比如這次的按鈕,要從buttonList中刪除。
2,給每個構造器添加objectindex,用來區分每個對象。
3,修改addChild方法,添加DisplayObject.parent = self,就是給每個自對象指定父級對象。


準備完畢,開始建立按鈕類LButton。

[javascript] view plain copy 
  1. function LButton(bitmap_up,bitmap_over){  
  2.     base(this,LSprite,[]);  
  3.     var self = this;  
  4.     self.type = "LButton";  
  5.     self.bitmap_up = bitmap_up;  
  6.     self.addChild(bitmap_up);  
  7.     if(bitmap_over == null){  
  8.         bitmap_over = bitmap_up;  
  9.     }else{  
  10.         self.addChild(bitmap_over);  
  11.     }  
  12.     self.bitmap_over = bitmap_over;  
  13.   
  14.   
  15.     self.bitmap_over.visible = false;  
  16.     self.bitmap_up.visible = true;  
  17.     LGlobal.buttonList.push(self);  
  18. }  
  19.   
  20.   
  21. LButton.prototype.buttonModeChange = function (){  
  22.     var self = this;  
  23.     var cood={x:0,y:0};  
  24.     var parent = self.parent;  
  25.     while(parent != "root"){  
  26.         cood.x += parent.x;  
  27.         cood.y += parent.y;  
  28.         parent = parent.parent;  
  29.     }  
  30.     if(self.ismouseon(LGlobal.mouseMoveEvent,cood)){  
  31.         self.bitmap_up.visible = false;  
  32.         self.bitmap_over.visible = true;  
  33.     }else{  
  34.         self.bitmap_over.visible = false;  
  35.         self.bitmap_up.visible = true;  
  36.     }  
  37. }  
  38. LButton.prototype.die = function (){  
  39.     var self = this;  
  40.     arguments.callee.super.die.call(this);  
  41.     for(var i=0;i<LGlobal.buttonList.length;i++){  
  42.         if(LGlobal.buttonList[i].objectindex == self.objectindex){  
  43.             LGlobal.buttonList.splice(i,1);  
  44.             break;  
  45.         }  
  46.     }  
  47. }  




看一下成果吧,看不到效果的請下載支援HTML5的瀏覽器

http://fsanguo.comoj.com/html5/jstoas06/index.html

用仿ActionScript的文法來編寫html5——第七篇,自訂按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.