2010-07-21 63 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

相关问题