2010-07-21 2 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 박스가되고 싶어요. 그래서 일단 클래스가 이제 용기와 동일하게이 클래스의 인스턴스를 고려할 수있는 무비 클립

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); 
+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 대단히 감사합니다. adamsboxmaker = new adasboxmaker()를 사용하여 비슷한 결과를 얻을 수있는 방법이 있습니까? ?? 나는 그런 식으로 클래스에 액세스하는 데 익숙해 져있다. – Adam

+0

@Adam – Allan

0

그냥 커플을 갈 수 있습니다.

모범 사례처럼 모든 함수는 클래스 생성자와 같은 반환 형식을 제외하고 모든 클래스는 대문자로 시작해야하며 대문자 여야합니다.

관련 문제