Skip navigation
Currently Being Moderated

Double Child added

Jul 31, 2012 12:46 AM

Tags: #air #flash #as3 #actionscript3

Hi

 

There are a lot of tutorials on how to use a class from your 'main stage' but not how to 'link' a class to a nother class if it's NOT the main class...??

 

Eg. I've created a class that extends TextInput and work beautifully but I have two childern added instead of 1, I know why this happens but don't know the correct way to 'call' my class that extends...

 

Call from one class to my setTextInputClass:

 

import setTextInputClass

   

var userName_TI:TextInput = new setTextInputClass(150,60,170,'a-zA-Z',false,'Test Text');

stage.addChild(userName_TI);/////   <-   I know this is wrong but what is the correct way....?

 

 

Separate calss:

 

package

{

import fl.controls.TextInput;

import flash.events.*;

import flash.text.TextFormat;

 

 

    public class setTextInputClass extends TextInput

    {

 

 

        private var TI:TextInput;

 

 

        private var TIX:Number;

        private var TIY:Number;

        private var TIW:Number;

        private var TIR:String;

        private var TIisP:Boolean;

        private var TIText:String;

 

        public function setTextInputClass(TIX,TIY,TIW,TIR,TIisP,TIText)

                    {

                              var TF1:TextFormat = new TextFormat();

                              TF1.color = 0x898888;

                              TF1.font = 'Calibri';

                              TF1.italic = true;

                              TF1.size = 13;

 

                              var TF2:TextFormat = new TextFormat();

                              TF2.color = 0x000000;

                              TF2.font = 'Calibri';

                              TF2.italic = false;

                              TF2.size = 13;

 

            TI = new TextInput();

                              TI.move(TIX,TIY);

                              TI.width = 170;

                              TI.restrict = (TIR)

                              TI.setStyle('textFormat', TF1);

 

 

                              TI.text = TIText;

 

                              TI.addEventListener(MouseEvent.CLICK, clickFun);

                              function clickFun(event:MouseEvent){TI.text = '';TI.setStyle('textFormat', TF2);}

 

                              TI.addEventListener(FocusEvent.FOCUS_OUT, outFun);

                              function outFun(event){

                                                                                          if(TI.text == ''){TI.text = TIText};

                                                                                          if(TI.text == TIText){TI.displayAsPassword = false;TI.setStyle('textFormat', TF1)};

                                                                                          }

 

                              TI.addEventListener(Event.CHANGE, isPassword);

                              function isPassword(e:Event):void{if (TI != TIText && TIisP == true){TI.displayAsPassword = true;}}

 

            addChild(TI);

        }

 

                    override public function get text():String

                    {

                              return TI.text;

                    }

 

                     override public function set text(str:String):void

                    {

                              TI.text = str;

                    }

    }

}

 
Replies
  • Currently Being Moderated
    Jul 31, 2012 4:29 AM   in reply to SaneDevil

    Without seeing the rest of the class code where you call the setTextInput class, I am only guessing that you are trying to target the stage before the stage is a known entity.  You probably need to use an ADDED_TO_STAGE listener in the calling class to wait until the stage exists for that class before trying to target the stage.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 5:45 AM   in reply to SaneDevil

    I only see one case of you trying to add the userName_TI instance as a child, that being the code you included in your second posting.  I think I understand what you are having a problem with though.  Inside setTextInputClass you are creating a second TextInput instance.  The one you are creating inside the class is the one you do not want.  The class itself is the TExtInput object you want since it extends the TExtInput class.  So anywhere you are using "TI" in your class code you probably want to replace that with either "this" or nothing, as in...

     

          this.move(TIX,TIY);

          this.width = 170;

          this.restrict = (TIR)

          this.setStyle('textFormat', TF1);

          this.text = TIText;

          this.addEventListener(MouseEvent.CLICK, clickFun);

        etc...

    or

     

          move(TIX,TIY);

          width = 170;

          restrict = (TIR)

          setStyle('textFormat', TF1);

          text = TIText;

          addEventListener(MouseEvent.CLICK, clickFun);

        etc...

     

     

    and you probably don't need the get / set functions

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points