Copy link to clipboard
Copied
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;
}
}
}
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Ned
Thanks for your time.
I create\Call a native window using a class and in that same class I use:
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....?
Which means I'm adding a child as above AND in my setTextInputClass , I don't know how to add a child by simply using my setTextInputClass class from another class(not using main fla) - sorry but I've never worked with classes before.
Basically if you create a class using my setTextInputClass an set is as the main class in the main FLA it only adds the child as expected - what I don't now is how to run the setTextInputClass script when it's NOT my main script(class)
I'm creating a app with differant windows (products\parts\clients etc) and want one single class to handle the text inputs for all these differant windows (each window is created by it's own class)
I hope this makes it a bit more clear on what I'm actually trying to achieve
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Nice, you are a genius!
I did try this before but your wording made me think of another approach for the setStyle which was giving errors when I tried that...
The following code works perfectly for any other folks...:
//USED TO CALL setTextInputClass CLASS\\
var userName_TI:TextInput = new setTextInputClass(150,60,170,'a-zA-Z',false,thisUserName);
//USED TO CALL setTextInputClass CLASS\\
package code
{
import flash.display.Sprite;
import flash.events.*;
import fl.controls.TextInput;
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;
this.move(TIX,TIY);
this.width = 170;
this.restrict = (TIR)
this.setStyle('textFormat', TF1);
this.text = TIText;
this.addEventListener(MouseEvent.CLICK, clickFun);
function clickFun(event:MouseEvent){
text = '';
setStyle('textFormat', TF2);
}
this.addEventListener(FocusEvent.FOCUS_OUT, outFun);
function outFun(event){
if(text == ''){text = TIText};
if(text == TIText){displayAsPassword = false;setStyle('textFormat', TF1)};
}
this.addEventListener(Event.CHANGE, isPassword);
function isPassword(e:Event):void{
if (text != TIText && TIisP == true){displayAsPassword = true;}
}
}
}
}