View Full Version : Input text/dynamic text
newbie50
12-22-2010, 01:06 PM
Hello,
I'm new to Actionscript 3.
I'm trying to get a user to enter his own numbers into an input text field.
And when he hits a button, it does a calculation ie. user input number x 5.
And that result shows up in a dynamic text field.
Is there a way to read a text input, do calculation and then take the result and turn it again into another string for the dynamic field?
Thanks.
chinchang
12-22-2010, 01:16 PM
Hi,
What you need is type-casting ( i.e. data type conversion)
Suppose you hv input textbox called 't'.
You can then do the following :
Number n = Number(t.text);
// process n
another_text.text = n + "";
marlopax
12-22-2010, 02:59 PM
This is AS2 code. Think you can able to convert in AS3.
var txtWidth = 100;
var txtHeight = 20;
var xPos = (Stage.width/2)-(txtWidth/2);
var yPos = 25;
var txt = this.createTextField("userTxt", this.getNextHighestDepth(), xPos, yPos, txtWidth, txtHeight);
txt.border = true;
txt.type = "input";
txt.restrict = "0-9";
var resultTxt = this.createTextField("result", this.getNextHighestDepth(), xPos, 0, txtWidth, txtHeight);
resultTxt.autoSize="Center";
var someListener:Object = new Object();
someListener.onSetFocus = function() {
if (userTxt.text == "Enter ID") {
userTxt.text = "";
}
};
Selection.addListener(someListener);
txt.onChanged = function() {
var maxLen = 5;
if (userTxt.text.length>maxLen) {
userTxt.text = userTxt.text.substr(0, maxLen);
}
};
//--------------------------------------------------//
var clip = this.createEmptyMovieClip("btn", this.getNextHighestDepth());
clip.beginFill(0x999999,100);
clip.moveTo(0,0);
clip.lineTo(100,0);
clip.lineTo(100,20);
clip.lineTo(0,20);
clip.lineTo(0,0);
clip.endFill();
clip._x = 50;
clip._y = 50;
clip.onRelease = function() {
clip._focusrect = false;
Selection.setFocus(clip);
if (userTxt.text != "") {
resultTxt.text=(userTxt.text*5);
} else {
userTxt.text = "Enter ID";
}
};
marlopax
newbie50
12-22-2010, 10:59 PM
Hello,
Thanks for the replies but not quite sure I understand.
Here's what I have so far.
inputBox.restrict="0-9\\.\\";
stage.focus = inputBox;
some_btn.addEventListener(MouseEvent.CLICK, userInput);
function userInput(evt:MouseEvent):void {
dynamicBox.text = (Number(inputBox * 2)).toString();
}
marlopax
12-23-2010, 12:54 PM
inputBox.restrict="0-9\\.\\";
stage.focus = inputBox;
some_btn.buttonMode=true;
some_btn.addEventListener(MouseEvent.CLICK, userInput);
function userInput(evt:MouseEvent):void {
dynamicBox.text = (Number(inputBox.text)*2).toString();
}
This will do the same as I post the previous code in AS2.
If you just paste the code in an empty AS2 fla and test, you must understand what it is.
marlopax
newbie50
12-27-2010, 02:12 AM
Thanks marlopax. It's just that I'm using Actionscript 3 and not 2. And I've been told the two are completely different.
newbie50
01-05-2011, 12:13 AM
Hello,
I've got the following to work without any error messages. Do you see any coding that should be cleaned up or done in a better way?
Thanks for the help.
inputBox.restrict="0-9\\.\\";
inputBox.text = "0.00";
stage.focus = inputBox;
outputBox.text = "0.00";
one_btn.addEventListener(MouseEvent.CLICK, userOne);
function userOne(evt:MouseEvent):void {
outputBox.text = (Number(inputBox.text) * 2 * 3).toString();
}
two_btn.addEventListener(MouseEvent.CLICK, userTwo);
function userTwo(evt:MouseEvent):void {
outputBox.text = (Number(inputBox.text) * 2 * 4).toString();
}
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.