ActionScript 3 - retrieving text values from TexInput created in component - actionscript-3

I have a tile layout containing a list of TextInputs and text fields , i have created these fields in a custom component using the following code;
var newTextInputs:Array = [];
var newTextLabels = [];
var space:Number = 30;
var count:Number = 0;
for (var i:String in columnsData)
{
//create text labels
var label:Text = new Text();
label.name = "label" + count;
label.text = i;
newTextLabels[count] = label;
addChild(newTextLabels[count]);
// create text fields
var field:TextInput = new TextInput();
field.name = "field" + count;
field.width = 100;
field.height = 25;
field.text = columnsData[i];
newTextInputs[count] = field;
addChild(newTextInputs[count]);
count++;
}
users are allowed edit the values in each TextInput field, now i need to retrieve the newly udpated values however how can i access these fields? Because the identifiers are created dynamically i cant simply go componentName.InputFieldName, any ideas?

I think what you're looking for is getChildByName
later edit: tested with Flash and TextField and it works:
trace(TextField(getChildByName('textfield')).text);

You can add an event handler for the TileList CHANGE event; when it fires, I think the event.target property will have the specific TextInput field. Alternatively you can look at the TileList.SelectedItem property.
You may also be able to have a DataProvider bound to the TileList instead of your code as shown, which will handle this automatically for you. Try just assigning your NewTextLabels array as the dataProvider.

Related

How would i make the buttons pressed in my calculator show up in my dynamic text box

I am making a calculator with a dynamic text box, buttons from 0-9, and +,-,/,*,=, and clear button. but for some reason everytime I press the buttons on my calculator, they don't show up in my dynamic text box like I need them to. My problem specifically is how do I make it that when the number buttons on my calculator are pressed, the numbers show up in my dynamic text box, like a proper calculator. I would really appreciate your help.
Here is my code:
import flash.events.MouseEvent;
var numbers:Array= [btnNum0,btnNum1,btnNum2,btnNum3,btnNum4,btnNum5,btnNum6,btnNum7,btnNum8,btnNum9];
var operations:Array = [btnAdd_,btnSubtract_,btnMultiply_,btnDivide_,btnEqual_,btnClear_];
var o:String;
var number1:Number;
var number2:Number;
function addListeners():void
{
for(var i:uint = 0; i < numbers.length; i++){
numbers[i].addEventListener(MouseEvent.CLICK, pressNumber);
}
for(i = 0; i < operations.length; i++){
operations[i].addEventListener(MouseEvent.CLICK, pressOperations);
}
btnClear.addEventListener(MouseEvent.CLICK, clearAll);
btnDot..addEventListener(MouseEvent.CLICK, addDot);
}
function pressNumber(event:MouseEvent): void
{
// find name of button pressed
var instanceName:String = event.target.name;
// get the number pressed fom the instanceName
var getNum = instanceName.charAt(6)
if(output.text == "0"){
output.text = "";
}
output.appendText(getNum);
}
function pressOperations(event:MouseEvent): void
{
var instanceName:String = event.target.name;
var currentOperator:String;
currentOperator = instanceName.slice(3,instanceName.indexOf("_"));
trace(currentOperator)
}
function clearAll(event:MouseEvent): void
{
output.text = "";
number1 = NaN;
number2 = NaN;
}
function addDot(event:MouseEvent): void
{
if(output.text.indexof(".") == -1){
output.appendText(".");
}
output.text = "0";
addListeners();
}
You shouldn't try to store information in the instance name. Use objects stored in arrays for the buttons. This is how you can handle everything like this.
Using objects stored in arrays to handle your buttons, the basic code to make a new button and set its properties (store information about the button in the instance):
var aButtons:Array = new Array();var i: int = 0;
for (i = 0; i < 10; i++) {
var mcButton: McButton = new McButton();
mcButton.iButtonValue = i;
mcButton.sButtonName = "Button Number " + i;
aButtons.push(mcButton);
}
Then you can reference the buttons by
aButtons[i]
aButtons[i].iButtonValue
You have first to set a value to your text field, because appendText() method can't append text to an empty text field (nothing will appear).
At the begining of your code:
output.text = '0';
To use property "name" you must set this property after creating instance. Like this:
var btnNum0:Button = new Button();
butNum0.name = "btnNum0";
If you don't do this "name" property will be empty that looks like your case.
UPDATE:
Helloflash is right in his comment to question. At first time I did not see that but in code more mistakes than I thought.
First of all you need to put off your init code from function addDot() to the begining of the code. At now this code will be never called because you try to add listeners into listener addDot().
output.text = "0";
addListeners();
Also change indexof to indexOf and remove extra dot from statement btnDot..addEventListener(MouseEvent.CLICK, addDot);. And big advice/request: use some formatting rules when you write a code (specially about brackets). Your code will be more clean and readable.

Flex: Change text value with loop and make image from every changed value?

I want to change taxt value from loop and every time I have changed it I take image from that.
I have two numeric steppers where other is start number and other is end number. I have also button that starts function.
So after pressed it starts:
private function makeFrames():void
{
for (var i:int = 0; i < endFrameNumber.value; i++)
{
currentFrameNumber++;
frameText.text = currentFrameNumber.toString();
makeImage(currentFrameNumber);
}
}
Then I make image from text:
private function makeImage(value:int):void
{
var projectFolderName:String = createFolder();
bitmapData = new BitmapData(frameText.width,frameText.height, true, 0x00ffffff);
bitmapData.draw(frameText,new Matrix());
var bitmap : Bitmap = new Bitmap(bitmapData);
var png:PNGEncoder = new PNGEncoder();
var ba:ByteArray = png.encode(bitmapData);
newImage = File.desktopDirectory.resolvePath(projectFolderName + "/" + "frame-number_" + value + ".png");
fileStream = new FileStream();
fileStream.open(newImage, FileMode.APPEND);
fileStream.writeBytes(ba);
fileStream.close();
}
So first I change text value and then I try to capture it. It make images yes, but all numbers is same. So how I can make it to change text value every time when I run "makeImage" in loop and it captures all images between start and end numbers??
Using Flash Builder 4.6 and AIR.
You should read about Flex Components Lifecycle.
So, there are two way to solve your problem:
Using invalidateProperties -> commitProperties.
addEventListener on Event.ENTER_FRAME
Sure you can't use foreach. In each enter frame you should:
Set new text
Invalidate text display
Make and save image

AS3 Getting which movieclip clicked on

So, I made a for loop to get several buttons in my project. It's a questionary, and I need to have a button to quickly select and navigate to any question there. I could do them all manually, but not only would my code be long and confusing, but also there are problems since there isn't always the same number of questions.
So right now I have:
function SetQuestionSquares():void{
for(var i:Number = 1; i <= TestProperties.QuestionLimit;i++){
var QuestionSquare:questionsquare = new questionsquare;
QuestionSquare.buttonMode = true;
QuestionSquare.mouseChildren = false;
QuestionSquare.x = NavLeft.x + (20 * i);
QuestionSquare.y = NavLeft.y;
QuestionSquare.questionsquaretext.text = i.toString();
addChild(QuestionSquare);
QuestionSquare.addEventListener(MouseEvent.CLICK, GoToQuestionNumber);
}
addChild(NavLeft);
addChild(NavRight);
}
function GoToQuestionNumber(e:MouseEvent):void{
WhichQuestion = ???; //I don't know what goes here.
UpdateQuestions();
trace("testing"); //Gets called correctly, so its working.
}
My problem is identifying which square was clicked. I need to have some way to grab the "e" (clicked) event, so I know which button the user clicked on.
You need .target property of the Event object:
WhichQuestion = e.target as questionsquare;
function GoToQuestionNumber(e:MouseEvent):void{
var WhichQuestion:DisplayObject = e.currentTarget as DisplayObject;
UpdateQuestions();
trace("testing");
}

AS3 multiple textfields made easy

I am working on a Results page for my game as well as upgrade page and looking for an easy way to do many textfields. I have a format for my text that takes care of font, colour, and size, but looking for an easy way to do the width and height of textfields to increase all at the same time.
I have been informed about a "with" keyword that may work but do not understand how to implement this within my program and essentially want to shorten my results class if possible.
Thank you,
The best way would be to create a custom function for generating textfield.
The example can be found in the livedocs itself.
So something like the following should suffice :
private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x;
result.y = y;
result.width = width;
result.height = height;
return result;
}
You may also set a default value to each attribute in the function.
private function createCustomTextField ( x:Number= <Default Value>, ...
Use it to add a textfield inside the container form.
var container:Sprite = new Sprite(); // New form container
container.addChild(createCustomTextField (20,20,50,50)); // Text Filed 1
container.addChild(createCustomTextField (20,50,50,50)); // Text Filed 2
addChild(container); // Add to current class
You may want to modify the function to accept a name so that each variable can be accessed later.
As far as I am aware, you can't use a "with" keyword to target multiple objects. Here's the documentation for it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#with
What I've done in the past is just make an array of all the targets, and then write a loop to apply properties to each:
var textFormat:TextFormat = generateMyCustomTextFormat();
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
//...
var textField3:TextField = new TextField();
var targets:Array = [textField1, textField2, textField3];
for(var i:int=0; i<targets.length; i++)
{
targets[i].defaultTextFormat = textFormat;
targets[i].width = 250;
//...
}

Creating multiple TextInput fields in for loop

I need to loop through an array and for each element create a textfield. My problem is how to create a new identifier for each new TextInput
this is my code;
var count:Number = 0;
for (var i:String in columnsData)
{
var myTI:TextInput = new TextInput();
myTI.width = 70;
myTI.height = 25;
myTI.text = columnsData[i];
myTI.name = "myTI" + count;
addChild(myTI);
count++;
}
all this does however is overwrite the previously created TextInput field, any ideas?
Try this:
var count:uint = 0,
textInputs:Array /* of TextInputs */ = [];
for(var i:String in columnsData){
textInputs[count] = new TextInput();
// Customize textInput[count] instead of myTI
addChild(textInputs[count]);
count++;
}
Outside of this loop, you should be able to look inside the textInputs array for references to each of your new TextInputs. Note that, inside the loop, you probably want to change the x/y coordinates for each TextInput so they don't overlap.