为什么我不能从自定义类中获取返回值?

时间:2010-07-21 23:22:58

标签: actionscript-3

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(参数);

并将txt作为返回的MovieClip中的显示对象。但那并没有发生。 有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

可能已经遗漏了几件小事。

作为最佳实践,对于类构造函数,所有函数都应该具有返回类型EXCEPT,并且所有类应该以大写形式开始并且是标题大小写。

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