How to get dynamic TextInput field values in Actionscript 3 - actionscript-3

Hi creating dynamic TextInput fields in a function. Need to get the values of those fields in another function. Can anyone throw some light on this.
for(var i:int=0;i<answers.length;i++)
{
txtbox = new spark.components.TextInput();
var lblBox:spark.components.Label = new spark.components.Label();
lblBox.id = "lbl"+i.toString();
lblBox.text = String(answersLabel.getItemAt(i) );
lblBox.width = 10
lblBox.x = xPos-15;
lblBox.y = yPos;
QuestionAnswer.addElement(lblBox);
txtbox.id = "text"+i.toString();
txtbox.x = xPos;
txtbox.y = yPos;
QuestionAnswer.addElement(txtbox);
xPos += 200;
}

var txt:TextField;
var i:uint;
var ary:Array = new Array();
function txtCreation ():void {
for( i=0;i<5;i++)
{
txt = new TextField();
txt.text = "txt"+i;
addChild(txt);
txt.x = 50 + txt.width *i;
txt.y = 20;
ary.push(txt);
}
}
txtCreation();
for(i=0;i<ary.length;i++)
{
trace("array values : " +ary[i].text);
}

Just look at the text variable for the textfield.
var textFromField : String = myInputText.text;

Related

How to get Text Input In Adobe AIR

I'm new to Adobe AIR and here is what i'm doing, (please note that i'm doing this by AS3 code:
Adding a VGroup to the Canvas control.
var vIncomeHeader:VGroup = new VGroup();
vIncomeHeader.width = 1326;
vIncomeHeader.height = 29;
vIncomeHeader.id = "vIncomeHeader";
canvasIncome.addChild(vIncomeHeader);
Creating and adding a HGroup to the VGroup created in step one.
Creating 5 TextInput and adding it to HGroup. First textbox is editable and rest are not editable.
var hfIncomeHeader:HGroup = new HGroup();
var txt:TextInput = new TextInput();
vIncomeHeader.addElement(hfIncomeHeader);
var lp:int =0;
var lp-inner:int =0;
for (lp=0; lp<10; lp++)
{
hfIncomeHeader = new HGroup();
hfIncomeHeader.width = 1326;
hfIncomeHeader.height = 29;
//Amount
txt = new TextInput();
txt.id = "txtAmount_" + lp;
txt.width = 120;
txt.height = 22;
txt.restrict = "0-9.\\";
txt.addEventListener(Event.CHANGE, txtChangeIncomeAmount);
hfIncomeHeader.addElement(txt);
for (lp-inner=0; lp-inner<4; lp-inner++)
{
//Income Type
txt = new TextInput();
txt.id = "txt_" + lp + "_" + lp-inner;
txt.width = 120;
txt.height = 22;
hfIncomeHeader.addElement(txt);
}
}
//txtChangeIncomeAmount
protected function txtChangeIncomeAmount(event:Object):void
{
// HOW TO Do IT
}
[Second and third steps are part of a for loop from 0 to 10]
Now, what I want to do is to write the number in first textbox and automatically fill the same number to other 4 textboxes of that HGroup.
You can set names to your TextInput and then get them by names. Something like that:
...
txt.name = "txt_" + lp-inner;
...
protected function txtChangeIncomeAmount(event:Event):void
{
var txt:TextInput;
for (var i:uint = 0; i < 4; i++)
{
txt = hfIncomeHeader.getChildByName("txt_" + i) as TextInput;
txt.text = (event.currentTarget as TextInput).text;
}
}

addchild with multiple movieclip in as3

var txt_mc:movieClip=new movieClip();
createTxt(3)
function createTxt(_no):void
{
var _sy = 0;
for (var i=0; i<_no; i++)
{
var txt:TextField = new TextField();
txt_fmt.size = _size;
txt.defaultTextFormat = txt_fmt;
//txt.autoSize = TextFieldAutoSize.CENTER;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.embedFonts = true;
txt.x = 0;
txt.y = _sy;
_sy = _sy + 25;
//txt.border = true
txt.text = "Enter your text here";
txt_mc.addChild(txt);
}
mc1.addChild(txt_mc);
mc2.addChild(txt_mc);
}
How can i addchild with multiple movieclip.
I was create a movieclip and wants to addchild in two movieclips which are located on stage.
please help me out.
I want to txt_mc will be add in mc1 and mc2 and from that code txt_mc is add in mc2 only.
You can't add the same instance of one movie clip in two different containers. But you can create two similar txt_mc instances and add them to mc1 and mc2.
UPDATE: So, you can modify your createTxt function, so it will return new instance of txt_mc each time you call it. And add it to every container you want:
function createTxt(_no):MovieClip
{
var txt_mc:movieClip=new MovieClip();
var _sy = 0;
for (var i=0; i<_no; i++)
{
var txt:TextField = new TextField();
txt_fmt.size = _size;
txt.defaultTextFormat = txt_fmt;
//txt.autoSize = TextFieldAutoSize.CENTER;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.embedFonts = true;
txt.x = 0;
txt.y = _sy;
_sy = _sy + 25;
//txt.border = true
txt.text = "Enter your text here";
txt_mc.addChild(txt);
}
return txt_mc;
}
mc1.addChild(createTxt(3));
mc2.addChild(createTxt(3));

AS3 creating dynamic Loader Names in a loop

EDITED for clarity: I want to load all kinds of images from an external url in a for loop.
I want to call them in a loop and when it's over I start the loop again. however I don't want to call them again with an "http request" rather I would like to loop through the loaded images over and over again.
Is it possible to create a dynamic Loader name in a loop?
Here is the code example:
var Count = 0;
// Loop Begins
var Count:Loader = new Loader();
Count.load(new URLRequest("myurl");
addChild(Count);
Count++;
// End Loop
Another example would be to have a NAME and just add the number on the end. But how
var Count = 0;
// Loop Begins
var MyName+Count:Loader = new Loader();
MyName+Count.load(new URLRequest("myurl");
addChild(MyName+Count);
Count++;
// End Loop
IN SHORT:
I want to load a bunch of images into an Array and loop through the loaded images by calling them later.
Thanks so much!
CASE1 code is how to load images in Sequence.
CASE2 code is how to load images in Synchronized.
First, the URL you want to import all images must be named sequentially.
for example Must be in the following format:
www.myURL.com/img0.jpg
www.myURL.com/img1.jpg
www.myURL.com/img2.jpg
www.myURL.com/img3.jpg
www.myURL.com/img4.jpg
www.myURL.com/img5.jpg
.
.
.
Try the code below, just a test.
CASE1
var imgLoader:Loader;
var imgRequest:URLRequest;
var count:int = -1;
const TOTAL_COUNT:int = 10;
var imgBox:Array = [];
var isCounting:Boolean;
function loadImage():void
{
count ++;
isCounting = true;
imgLoader = new Loader();
imgRequest = new URLRequest();
imgRequest.url = "www.myURL.com/img" + count +".jpg";
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
if(count == TOTAL_COUNT)
{
isCounting = false;
count = -1;
}
}
function onLoadedImg(e:Event):void
{
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bmp.x = Math.random() * width;
bmp.y = Math.random() * height;
bmp.width = 100;
bmp.height = 100;
this.addChild(bmp);
imgBox.push(bmp);
if( isCounting == false)
{
return;
}
loadImage();
}
function unloadedImg(e:IOErrorEvent):void
{
imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
trace("load Failed:" + e);
}
loadImage();
CASE2
var imgLoader:Loader;
var imgRequest:URLRequest;
const TOTAL_COUNT:int = 10;
var imgBox:Array = [];
function loadImage2():void
{
for(var i:int = 0; i<TOTAL_COUNT; i++)
{
imgLoader = new Loader();
imgRequest = new URLRequest();
imgRequest.url = "www.myURL.com/img" + i +".jpg";
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
}
}
function onLoadedImg(e:Event):void
{
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bmp.x = Math.random() * width;
bmp.y = Math.random() * height;
bmp.width = 100;
bmp.height = 100;
this.addChild(bmp);
imgBox.push(bmp);
}
function unloadedImg(e:IOErrorEvent):void
{
imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
trace("load Failed:" + e);
}
loadImage2();
If you want access loaded Image. refer a following code. If you do not put them in an array can not be accessed in the future.
for(var int:i=0; i<TOTAL_COUNT; i++)
{
var bitmap:Bitmap = imgBox[i] as Bitmap;
trace("index: " + i + "x: " + bitmap.x + "y: " + bitmap.y, "width: " + bitmap.width + "height: " + bitmap.height);
}
Now that we're on the same page:
var imgArray:Array = new Array;
var totalImages:int = 42;
var totalLoaded:int = 0;
var loaded:Boolean = false;
function loadImages():void{
for(var count:int = 0; count < totalImages; count++){
var image:Loader = new Loader();
image.load(new URLRequest("image" + i + ".jpg");
image.addEventListener(Event.COMPLETE, loaded);
imgArray.push(image);
}
}
function loaded(e:Event):void{
totalLoaded++;
if (totalLoaded == totalImages){
loaded = true;
}
}
function displayImages():void{
if (loaded){
for(var i:int = 0; i < imgArray.length(); i++){
addChild(imgArray[i]);
}
}
}

Two colors in one text field using Actionscript 3

Is it possible to have two text colors in one text field using Actionscript 3.0?
ex: how can i make like the first string black and the second string red?
Here is my code when using a single color:
public function logs(txt)
{
if (txt == '')
{
textLog.text = "Let's Open up our treasure boxes !!!";
}
else
{
textLog.text = '' + txt + '';
}
textLog.x = 38.60;
textLog.y = 60.45;
textLog.width = 354.50;
textLog.height = 31.35;
textLog.selectable = false;
textLog.border = false;
var format:TextFormat = new TextFormat();
var myFont:Font = new Font1();
format.color = 0x000000;
format.font = myFont.fontName;
format.size = 18;
format.align = TextFormatAlign.CENTER;
format.bold = false;
textLog.embedFonts = true;
textLog.setTextFormat(format);
this.addChild(textLog);
}
In setTextFormat you can specify start index and end index. You can also render text as html using textLog.htmlText.
First set the text
var t:TextField = new TextField();
t.text = "BLUEGREEN";
addChild(t);
Then method 1
var format1:TextFormat = t.getTextFormat(0, 4);
format1.color = 0x0000ff;
t.setTextFormat(format1, 0, 4);
var format2:TextFormat = t.getTextFormat(5, t.length);
format2.color = 0x00ff00;
t.setTextFormat(format2, 5, t.length);
Or method 2
t.htmlText = '<font color="#0000ff">BLUE</font><font color="#00ff00">GREEN</font>';
If you want to do like that, you need create a function to control. charAt(DEFINE INDEX OF STRING HERE).
var format2:TextFormat = textbox.defaultTextFormat;
format2.color = 0x000000;
textbox.defaultTextFormat = format2;
if((textbox.text.charAt(3) == "d") && ( textbox.text.charAt(4) == "i")){
var format1:TextFormat = textbox.defaultTextFormat;
format1.color = 0xFF0000;
textbox.setTextFormat(format1, 3, 5);}
else{
textbox.setTextFormat(textbox.defaultTextFormat);}

How to skew a Textfield in Actionscript 3.0?

How can I skew a Textfield in order to set a ascending space for each line.
Following image should illustrate my intention:
Got it:
adobe
public class TextBlock_createTextLineExample extends Sprite {
public function TextBlock_createTextLineExample():void {
var str:String = "I am a TextElement, created from a String and assigned " +
"to the content property of a TextBlock. The createTextLine() method " +
"then created these lines, 300 pixels wide, for display." ;
var fontDescription:FontDescription = new FontDescription("Arial");
var format:ElementFormat = new ElementFormat(fontDescription);
format.fontSize = 16;
var textElement:TextElement = new TextElement(str, format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
createLines(textBlock);
}
private function createLines(textBlock:TextBlock):void
{
var lineWidth:Number = 300;
var xPos:Number = 15.0;
var yPos:Number = 20.0;
var textLine:TextLine = textBlock.createTextLine (null, lineWidth);
while (textLine)
{
//increase textLine every Line 10 px to get an offset
textLine.x += 10
textLine.y = yPos;
yPos += textLine.height + 2;
addChild (textLine);
textLine = textBlock.createTextLine (textLine, lineWidth);
}
}
}
}
private static const _DEG2RAD:Number = Math.PI/180;
public static function skew(target:DisplayObject, skewXDegree:Number, skewYDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.b = Math.tan(skewYDegree*_DEG2RAD);
m.c = Math.tan(skewXDegree*_DEG2RAD);
target.transform.matrix = m;
}
public static function skewY(target:DisplayObject, skewDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.b = Math.tan(skewDegree*_DEG2RAD);
target.transform.matrix = m;
}
public static function skewX(target:DisplayObject, skewDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.c = Math.tan(skewDegree*_DEG2RAD);
target.transform.matrix = m;
}