2010-07-21 5 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;Pourquoi ne puis-je pas obtenir une valeur retournée par ma classe personnalisée?

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; 

    } 

} 

}

Ceci est ma première fissure à écrire une classe et après avoir lu c'est ce que j'ai. Essentiellement, je veux être capable d'écrire var txt: adamsboxmaker = new adamsboxmaker (paramètres);

et que txt soit un objet d'affichage provenant du MovieClip retourné. Mais ça ne se passe pas. Quelqu'un peut-il me diriger dans la bonne direction?

Répondre

3

Ok. Donc, le problème ici est un petit malentendu. Vous créez une instance d'une classe adamsboxmaker et la référence est automatiquement renvoyée et stockée dans la variable txt. C'est ainsi que fonctionnent les langages orientés objet.

Ce que vous essayez d'utiliser est d'utiliser une méthode usine pour créer un objet. Pour mettre en œuvre cela, changer votre createBox à une fonction statique publique

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){ 

et de supprimer l'appel à l'intérieur du constructeur.

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 

} 

Ensuite, tout ce que vous devez faire est

txt = adamsboxmaker.createBox(paramters); 

Re: Question au commentaire

Dans ce cas, vous voulez que votre adamsboxmaker soit la boîte. Alors d'abord faire la étendre la classe MovieClip

public class adamsboxmaker extends MovieClip 
{ 

Vous pouvez maintenant considérer l'instance de cette classe pour être le même que le conteneur: MovieClip que vous créez. Ajouter ce code dans le constructeur:

 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; 
    } 

Maintenant vous pouvez aller

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

Merci beaucoup. Existe-t-il un moyen d'obtenir des résultats similaires en faisant adamsboxmaker = new adasboxmaker(); ?? Im s'habituer à accéder aux classes de cette façon – Adam

+0

@Adam yep voir ma réponse mise à jour – Allan

0

Juste quelques petites choses à ajouter qui auraient pu être manqués.

Comme meilleure pratique, toutes les fonctions doivent avoir un type de retour EXCEPT pour les constructeurs de classes comme et toutes les classes doivent démarrer majuscules et être le cas du titre.

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

Questions connexes