2010-07-21 66 views
0
package com.adam.etutorial 

import flash.display.MovieClip; import flash.text.TextField; import flash.display.Sprite; import flash.text.TextFormat; import flash.display.Shape;爲什麼我無法從我的自定義類中獲取返回的值?

public class adamsboxmaker 
{ 

    //(boxWidth, boxHeight, lineColour, lineThickness, beginFillColour, fillIf, fontcolour, fontsize, fonttype, textFormat, textWidth, textHeight, text, Xoffset, Yoffset, textIf) 
    public function adamsboxmaker(boxWidth:Number,boxHeight:Number,lineColour:Number,lineThickness:int, beginFillColour:Number, fillIf:Boolean, fontColour:Number, fontSize:int, fontType:String, textWidth:Number, textHeight:Number, txt:String, Xoffset:Number, Yoffset:Number, textIf:Boolean) 
    { 

     createBox(boxWidth,boxHeight,lineColour,lineThickness, beginFillColour, fillIf, fontColour, fontSize, fontType, textWidth, textHeight, txt, Xoffset, Yoffset, textIf); 


    } 

    private function createBox(boxWidth:Number,boxHeight:Number,lineColour:Number,lineThickness:int, beginFillColour:Number, fillIf:Boolean, fontColour:Number, fontSize:int, fontType:String, textWidth:Number, textHeight:Number, txt:String, Xoffset:Number, Yoffset:Number, textIf:Boolean) 
    { 


     /*BUILD CONTAINER*/ 
     var container:MovieClip = new MovieClip(); 
     /*END CONTAINER*/ 

     /*BUILD BOX*/ 
     var theBox:Shape = new Shape(); 
     container.addChild(theBox); 
     theBox.graphics.lineStyle(lineThickness, lineColour); 

     if (fillIf == true) 
     { 
      theBox.graphics.beginFill(beginFillColour); 
     } 

     theBox.graphics.moveTo(0, 0); 
     theBox.graphics.lineTo(boxWidth, 0); 
     theBox.graphics.lineTo(boxWidth, boxHeight); 
     theBox.graphics.lineTo(0, boxHeight); 
     theBox.graphics.lineTo(0, 0); 

     if (fillIf == true) 
     { 
      theBox.graphics.endFill(); 
     } 
     /*END BOX*/ 

     if (textIf == true) 
     { 
      /*BUILD FORMATTING*/ 
      var myFormat:TextFormat = new TextFormat(); 
      myFormat.color = fontColour; 
      myFormat.size = fontSize; 
      myFormat.font = fontType; 
      /*END FORMATTING*/ 

      /*BUILD TEXTFIELD*/ 
      var theText:TextField = new TextField(); 
      theText.text = txt; 
      theText.x = Xoffset; 
      theText.y = Yoffset; 
      theText.width = textWidth; 
      theText.height = textHeight; 
      theText.wordWrap = true; 
      theText.setTextFormat(myFormat); 
      container.addChild(theText); 
      /*END TEXTFIELD*/ 
     } 
     container.visible = false; 

    return container; 

    } 

} 

}

這是我在寫一個類,並閱讀了這是我第一次後裂紋。基本上,我希望能夠寫var txt:adamsboxmaker = new adamsboxmaker(parameters);

並將txt作爲返回的MovieClip的顯示對象。但這並沒有發生。 有人能指引我正確的方向嗎?

回答

3

好的。所以這裏的問題有點誤解。您正在創建一個adamsboxmaker類的實例,然後該參考會自動返回並存儲在txt變量中。這就是面嚮對象語言的工作原理。

你要做的是使用工廠方法來創建一個對象。要實現這一點,請將您的createBox更改爲公共靜態函數

public static function createBox(boxWidth:Number,boxHeight:Number,lineColour:Number,lineThickness:int, beginFillColour:Number, fillIf:Boolean, fontColour:Number, fontSize:int, fontType:String, textWidth:Number, textHeight:Number, txt:String, Xoffset:Number, Yoffset:Number, textIf:Boolean){ 

並從構造函數中刪除調用。

public function adamsboxmaker(boxWidth:Number,boxHeight:Number,lineColour:Number,lineThickness:int, beginFillColour:Number, fillIf:Boolean, fontColour:Number, fontSize:int, fontType:String, textWidth:Number, textHeight:Number, txt:String, Xoffset:Number, Yoffset:Number, textIf:Boolean) 
{ 

      //removed call 

} 

然後,所有你需要做的是

txt = adamsboxmaker.createBox(paramters); 

回覆:在評論

在這種情況下,問你希望你的adamsboxmaker是框。所以,首先使類擴展MovieClip的

public class adamsboxmaker extends MovieClip 
{ 

您現在可以考慮這個類的實例是一樣的容器:MovieClip的你創造。下面的代碼添加到構造函數:

 public function adamsboxmaker(boxWidth:Number,boxHeight:Number,lineColour:Number,lineThickness:int, beginFillColour:Number, fillIf:Boolean, fontColour:Number, fontSize:int, fontType:String, textWidth:Number, textHeight:Number, txt:String, Xoffset:Number, Yoffset:Number, textIf:Boolean){ 

        var theBox:Shape = new Shape(); 
        addChild(theBox); //we add it to this, rather than a container 
        theBox.graphics.lineStyle(lineThickness, lineColour); 

        if (fillIf == true) 
        { 
         theBox.graphics.beginFill(beginFillColour); 
        } 

        theBox.graphics.moveTo(0, 0); 
        theBox.graphics.lineTo(boxWidth, 0); 
        theBox.graphics.lineTo(boxWidth, boxHeight); 
        theBox.graphics.lineTo(0, boxHeight); 
        theBox.graphics.lineTo(0, 0); 

        if (fillIf == true) 
        { 
         theBox.graphics.endFill(); 
        } 
        /*END BOX*/ 

        if (textIf == true) 
        { 
         /*BUILD FORMATTING*/ 
         var myFormat:TextFormat = new TextFormat(); 
         myFormat.color = fontColour; 
         myFormat.size = fontSize; 
         myFormat.font = fontType; 
         /*END FORMATTING*/ 

         /*BUILD TEXTFIELD*/ 
         var theText:TextField = new TextField(); 
         theText.text = txt; 
         theText.x = Xoffset; 
         theText.y = Yoffset; 
         theText.width = textWidth; 
         theText.height = textHeight; 
         theText.wordWrap = true; 
         theText.setTextFormat(myFormat); 
         container.addChild(theText); 
         /*END TEXTFIELD*/ 
        } 
        visible = false; 
    } 

現在你可以去

txt = new adamsboxmaker(parameters); 
addChild(txt); 
+0

非常感謝你。 有沒有辦法通過adamsboxmaker = new adasboxmaker()獲得類似的結果。 ??林習慣訪問類的方式 – Adam 2010-07-22 00:07:07

+0

@亞當是看到我更新的答案 – Allan 2010-07-22 00:23:26

0

只是幾個小東西補充一點,可能已經錯過。

作爲最佳實踐,所有函數都應該具有返回類型,除了類構造函數之外,所有類都應該以大寫字母開頭並且是標題大小寫。

public class AdamsBoxMaker 
{ 
    public function AdamsBoxMaker() 
    { 
    //your constructor 
    } 

    public function anotherMethod(someParameter : String) : String 
    { 
    //returns a String 
    return someParameter += " awesome!"; 
    } 

    private function anotherPrivateMethod(someParameter : String) : void 
    { 
    //returns nothing 
    } 

HTH

相關問題