AS3 Number from input text to dynamic text
Copy link to clipboard
Copied
I'm having trouble passing a number from an input text box to a dynamic text box after a button is clicked.
In timeline I have two frames labeled: 1) inf, which starts with an input text box and a button, and 2) closing, which receives data from inf section.
INF SECTION has an input text box (age_txt) and a button (inf_bt).
var userAge:Number;
age_txt.text;
inf_bt.addEventListener(MouseEvent.CLICK, gotoPg1);
function gotoPg1 (evt:MouseEvent): void
{
age_txt.text=String(userAge);// user age
gotoAndStop("pg1");
}
CLOSING SECTION has dynamic text box (user_age_txt). I get NaN, but not the age.
user_age_txt.text = String (userAge);
Can someone tell me what's wrong with my code and how to fix it?
Thanks
German
Copy link to clipboard
Copied
It's hard to follow what you are doing between your description and your use of three different identities for what appears to be the same value. The "userAge" variable doesn't appear to be assigned any value in your code.
Copy link to clipboard
Copied
Ned:
Thanks for replying. Maybe I'm wrong with my proposed code, but input text box (age_txt) gets the age from the user, and that is the number I want to pass to use_age_txt (dynamic text box).
When I have used var userAge:Number=0;
I get the zero passed to user_age_text (dynamic text box).
Thanks again
German
Copy link to clipboard
Copied
in your first function, you should change the order from -
age_txt.text=String(userAge);// user age
to -
userAge = parsetInt(age_txt.text); // user age
the 'parseInt' will turn the string to a integer. now your userAge var will have value.
Copy link to clipboard
Copied
TheErez:
Thanks for your suggestion. I changed the order as suggested:
INF SECTION
var userAge:int; // variable
age_txt.text; // input text
inf_bt.addEventListener(MouseEvent.CLICK, gotoPg1); // button
function gotoPg1 (evt:MouseEvent): void
{
userAge =parseInt(age_txt.text);// user age
gotoAndStop("pg1");
}
CLOSING SECTION
When trying to retreive the number through user_txt-age (dynamic box) I have unsuccessfully used:
user_age_txt_text = userAge;
user_age_txt_text = parseInt (userAge);
user_age_txt_text = age_txt.text;
userAge= parseInt(user_age_txt.text);
Can you please suggest how to retrieve the number from input text box age_txt?
German
Copy link to clipboard
Copied
you have me confused now:
if what you want to achieve is to have the user type his age into a textfield on the first frame, click a button and then print it into a textfiled on the second frame, what you should do is as follows:
INF SECTION
// on the stage there's a button called 'inf_bt' and a textfield ('input' type) called age_txt
var userAge:int; // variable
inf_bt.addEventListener(MouseEvent.CLICK, gotoPg1); // button
function gotoPg1 (evt:MouseEvent): void
{
userAge =parseInt(age_txt.text);// user age
gotoAndStop("pg1");
}
CLOSING SECTION
// on the stage there should be a textfield ('dynamic' type) called 'user_age_txt'
user_age_txt.text = userAge;
i'd recommend to you to change the textfields' names into something that would be less confusing, such as 'age_input_txt' and 'age_output_txt'
Copy link to clipboard
Copied
Okay, so I have the same problem! But I think I have come up with an idea but, for me, it still is coming up with an error and I'm trying to fix that. One note this WILL NOT work if you want to let people type letters as well unless you have time to copy and paste for every letter of the alphabet. The way is to turn your text into dynamic and make it a button, okay, then call you button what ever you want but I am going to use A1 standing for Answer 1. Then go into the Actions tab and type this:
stop();
import flash.events.*;
import flash.text.TextField;
import flash.ui.*
import flash.display.DisplayObjectContainer
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
stage.addEventListener(Event.ENTER_FRAME,OnFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,this.OnKeyDown);
A1.addEventListener(MouseEvent.CLICK, A1lick);
//these are some of the variables used
var swapper1000:Boolean = false;
var doc1:DisplayObjectContainer;
var An1:TextField;
var doc2:DisplayObjectContainer;
var An2:TextField;
var doc:DisplayObjectContainer;
var An3:TextField;
//this finds the text field (I'm still having problems even though it works of one of my other codes)
doc1 = A1.overState as DisplayObjectContainer;
An2 = doc1.getChildAt(1) as TextField;
doc2 = A1.downState as DisplayObjectContainer;
An3 = doc2.getChildAt(1) as TextField;
doc = A1.upState as DisplayObjectContainer;
An1 = doc.getChildAt(1) as TextField;
function A1lick(event:MouseEvent):void
{
//this senses if you have clicks the text so you can start writing in it
if(swapper1000)
{
swapper1000 = false
}
else
{
swapper1000 = true
}
}
function OnFrame(e:Event)
{
//you can use this for some random stuff
}
function OnKeyDown(e:KeyboardEvent)
{
if(swapper1000)
{
//lets you right numbers
if(e.keyCode == Keyboard.NUMBER_1)
{
var back:String = An1.text; //holds what it typed into the text field already so it doesn't just change all the text to a 1
An1.text = back"'1";
An2.text = back+"1";
An3.text = back+"1"; //changes text for every state of the button
}
//You can repeat the Number_1 thing so you have all the numbers but change the '1's to 2,3,4... and so on
//and you might want to do one for the 'enter' button because some people like to press enter once they have finished typing. The code for that would be something like this:
if(e.keyCode == Keyboard.ENTER)
{
swapper1000 = false;
}
}
}
this is my way of looking at it, I know it is a long way around the problem but it is the first thing that came into my mind and it is possible to do it this way but I am still getting errors but you may not or you may find the problem and fix it!
Copy link to clipboard
Copied
Quick Update! I have made it work! Just one thing when you are creating the button MAKE SURE all of the frames are keyframes (the Up, Over, Down and Hit frames!)
Copy link to clipboard
Copied
Just realised the only problem with this is that if you add in backspace
e.g
if(e.keyCode == Keyboard.BACKSPACE)
{
An1.text = '';
An2.text = '';
An3.text = '';
}
you will have to clear the text field unless you find a way to do it slightly differently where you could actually minus the last letter from it but it would take a ton of variables and coding to do so (In my mind). So if you find another way great! But that is how to do it and I have got the same code working!
Copy link to clipboard
Copied
One more thing (Sorry) sure you have a background to your button otherwise it will NOT work... Just tried it and it comes up with an error

