Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 为什么可以';我不能从自定义类中获取返回值吗?_Actionscript 3 - Fatal编程技术网

Actionscript 3 为什么可以';我不能从自定义类中获取返回值吗?

Actionscript 3 为什么可以';我不能从自定义类中获取返回值吗?,actionscript-3,Actionscript 3,{ 导入flash.display.MovieClip; 导入flash.text.TextField; 导入flash.display.Sprite; 导入flash.text.TextFormat; 导入flash.display.Shape package com.adam.etutorial } 这是我第一次尝试写一堂课,读完后,这就是我所拥有的 本质上,我希望能够编写var txt:adamsboxmaker=newadamsboxmaker(参数) 并将txt作为返回的MovieC

{ 导入flash.display.MovieClip; 导入flash.text.TextField; 导入flash.display.Sprite; 导入flash.text.TextFormat; 导入flash.display.Shape

package com.adam.etutorial
}

这是我第一次尝试写一堂课,读完后,这就是我所拥有的

本质上,我希望能够编写var txt:adamsboxmaker=newadamsboxmaker(参数)

并将txt作为返回的MovieClip中的显示对象。但事实并非如此。
有人能给我指一下正确的方向吗?

好的。所以这里的问题是一点误解。您正在创建AdamBoxMaker类的实例,然后该引用将自动返回并存储在txt变量中。这就是面向对象语言的工作原理

您试图做的是使用工厂方法来创建对象。要实现此功能,请将createBox更改为公共静态函数

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;

    }

}
并从构造函数内部删除调用

 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

}
Re:评论中的问题

在本例中,您希望AdamBoxMaker成为长方体。因此,首先让学生扩展MovieClip

txt = adamsboxmaker.createBox(paramters);

现在您可以考虑这个类的实例与容器:您创建的MOVICELIP相同。将此代码添加到构造函数中:

public class adamsboxmaker extends 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;
     }

嗯。所以这里的问题是一点误解。您正在创建AdamBoxMaker类的实例,然后该引用将自动返回并存储在txt变量中。这就是面向对象语言的工作原理

您试图做的是使用工厂方法来创建对象。要实现此功能,请将createBox更改为公共静态函数

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;

    }

}
并从构造函数内部删除调用

 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

}
Re:评论中的问题

在本例中,您希望AdamBoxMaker成为长方体。因此,首先让学生扩展MovieClip

txt = adamsboxmaker.createBox(paramters);

现在您可以考虑这个类的实例与容器:您创建的MOVICELIP相同。将此代码添加到构造函数中:

public class adamsboxmaker extends 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);

HTH

只需补充几件可能遗漏的小事

作为最佳实践,除了类构造函数(如)之外,所有函数都应该有一个返回类型,并且所有类都应该以大写开头,并且是标题大小写

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

HTH

非常感谢。是否有一种方法可以通过执行adamsboxmaker=new adasboxmaker();?获得类似的结果??我已经习惯了那样上课了,非常感谢。是否有一种方法可以通过执行adamsboxmaker=new adasboxmaker();?获得类似的结果??我已经习惯了用这种方式访问类