2009-06-23 8 views
1

我對flash和actionscript 3很新穎。我一直在閱讀很多,這也是我第一次面向對象編程。我正在寫AS3嗎?

到目前爲止,我使用登錄按鈕創建了一個應用程序,就這些。但是,我想知道我做錯了什麼樣的事情,或者應該做什麼不同(或更好)。我使用Adobe的Flex Builder 3

主要ActionScript文件是Client2.as:

package 
{ 
    //import required libraries 
    import flash.display.Sprite; 

    //set project properties 
    [SWF(width="800", height="600", frameRate="31", backgroundColor="#C0C0C0")] 

    //launch main class 
    public class Client2 extends Sprite 
    { 
     public function Client2() { //the constructor 
      trace("Client launched."); 
      var loginGui:LoginInterface = new LoginInterface(); //load the login interface object 
      loginGui.init(); //initialize the login interface (load it) 
      addChild(loginGui); //add login gui to the display tree 
     } 
    } 
} 

它加載登錄界面對象。這是一件好事,我正確地做這件事嗎?

然後有LoginInterface.as類文件:

package 
{ 
    //import required libraries 
    import flash.display.Sprite; 

    //the LoginInterface class 
    public class LoginInterface extends Sprite 
    { 
     public function LoginInterface() //the constructor 
     { 
      trace("LoginInterface object loaded."); 
     } 

     public function init():void //initialize the login interface (load it) 
     { 
      trace("LoginInterface init method was called."); 
      var loginButton:CustomButton = new CustomButton(300, 300, 100, 30, 3, 18, "Login!"); //create a new custom button 
      addChild(loginButton); //add the custom button to the display tree 
     } 
    } 
} 

怎麼樣?任何意見?爲了使創建簡單的按鈕更容易一點,我則創造了另一個名爲CustomButton.as類文件 - >

package 
{ 
    import flash.display.SimpleButton; 
    import flash.display.Sprite; 
    import flash.text.TextField; 
    import flash.text.TextFormat; 
    import flash.text.TextFormatAlign; 

    public class CustomButton extends Sprite 
    { 
     public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String) 
     { 
      //create new simple button instance 
      var myButton:SimpleButton = new SimpleButton(); 
      //create the look of the states 
      var normal:Sprite = new Sprite(); 
      normal.graphics.lineStyle(1, 0x000000); 
      normal.graphics.beginFill(0x6D7B8D); 
      normal.graphics.drawRect(xLoc, yLoc, width, height); 
      //the mouseover sprite 
      var over:Sprite = new Sprite(); 
      over.graphics.lineStyle(1, 0x000000); 
      over.graphics.beginFill(0x616D7E); 
      over.graphics.drawRect(xLoc, yLoc, width, height); 
      // assign the sprites 
      myButton.upState = normal; 
      myButton.downState = normal; 
      myButton.hitTestState = normal; 
      myButton.overState = over; 
      //add the button to the display tree 
      addChild(myButton); 

      //create button label 
      var tText:TextField = new TextField(); 
      tText.mouseEnabled = false, 
      tText.x = xLoc; 
      tText.y = yLoc + iLabelOffset; 
      tText.width = width; 
      tText.selectable = false 
      var Format:TextFormat = new TextFormat(); 
      Format.font = "Arial"; 
      Format.color = 0x000000; 
      Format.size = fontsize; 
      Format.bold = false; 
      Format.align = TextFormatAlign.CENTER; 
      tText.defaultTextFormat = Format; 
      tText.text = label; 
      addChild(tText) 
     } 
    } 
} 

有什麼對此發表評論?我確信我做了很多錯誤的事情,也許我沒有真正瞭解整個面向對象的事情?另外,對於我在類聲明後使用「extends ...」的方式我有一種不好的感覺,主要是因爲我一直在使用Sprite,並不真正瞭解它爲什麼或者它做了什麼(遇到問題也在互聯網上找到)。我不確定的另一件事是AS3中變量的命名。我真的應該使用xLoc還是iLabelOffset?我認爲我的變量atleast命名不太一致?

我希望有人能給我一個比現在更好的軌道,因爲我相信我應該在繼續研究這個野獸之前改進我的AS3編碼。

非常感謝。

+0

很高興看到幀率31神話活着和踢! – spender 2009-06-23 23:15:21

回答

3

我的看法:

一個名爲Client2的類可能是一個錯誤的命名選擇。 Client2並沒有告訴我很多。一年內它會告訴你多少?

在CustomButton中,初始化是在構造函數中處理的。在LoginInterface中,使用類的實例需要對init()的顯式調用。容易忘記和不必要的。除非有很好的理由不從構造函數調用init。

iLabelOffset是什麼意思?最好在參數列表中使用較少混淆的名稱。

CustomButton構造函數的參數列表很長。沒有必要傳入x和y。 Sprite已經有一個x和y屬性,所以把所有東西都放回到一個零偏移量,並且在構建完成後,操縱CustomButton的x和y屬性。

在CustomButton構造函數的其餘參數中,考慮對它們進行重新排序,以便您可以提供默認參數(只能在參數列表的末尾)。 labelOffset和fontSize看起來很不錯。

通過刪除重複代碼來保持函數大小很小。創建一個函數來創建按鈕狀態Sprites,它在參數中使用一個顏色(或者更好,將這個功能移動到一個新類型的Sprite派生類中),並且添加一個createLabel函數,以便您可以將該代碼移出構造函數。如果您嘗試保持功能小巧,您的代碼將變得更易於閱讀和維護。這也意味着你不得不寫更少的評論;-)

2

斯彭德擊中了頭部。這些肯定是我查看代碼時引發的問題。他提到的事情並不是非常糟糕的Actionscript問題,(問題不太合適,也許是「需要注意的地方」),這些都是所有編程語言的普遍問題。例如描述性命名非常重要。

有幾本書關注編程的這一面,更少的書能夠很好地實現。我會強烈建議拿起以下兩本書,如果你想在這方面越長,(我會推薦它,即使你不想太:)

Code Complete

The pragmatic programmer

這兩本書都是每個程序員都應該閱讀的,所以請檢查一下。你從Actionscript的角度來看是好的,但這只是語法。值得注意的是,除非你真的寫代碼,否則這些技能永遠不會發展,所以通過「繼續在這個野獸上繼續工作」,其餘的將會跟隨。

0

就像樣式一樣,我喜歡在構造函數之外聲明我的變量。這有助於我感覺我不會對公共與私人或範圍產生任何意外。另外請注意添加的空白區域,它可以提高可讀性。

public class CustomButton extends Sprite 
{ 
    private var myButton:SimpleButton; 
    private var normal:Sprite; 
    private var over:Sprite; 
    // etc ... 

    public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String) 
    { 
      //create new simple button instance 
      myButton = new SimpleButton(); 

      //create the look of the states 
      normal = new Sprite(); 
      normal.graphics.lineStyle(1, 0x000000); 
      normal.graphics.beginFill(0x6D7B8D); 
      normal.graphics.drawRect(xLoc, yLoc, width, height); 

      //the mouseover sprite 
      over = new Sprite(); 
      over.graphics.lineStyle(1, 0x000000); 
      over.graphics.beginFill(0x616D7E); 
      over.graphics.drawRect(xLoc, yLoc, width, height); 

      // etc ...