PDA

View Full Version : AS2: Input text to integer?




epicnyancat
Jun 3, 2013, 04:08 PM
I'm trying to make an app in Flash Actionscript 2 that includes multiplying an input text by eight. There are six text boxes; half are input and half are dynamic. When the user inputs a number into one, the output in the corresponding one should change to eight times the original amount. (However, the middle text box and its corresponding text box stay equal to each other.)

I don't want the user to have to press a button all the time, so outside the frame I put a "control panel" movieclip. It basically has two frames, one with the tasks I want, so it basically performs them 12 times per second at 24 fps.

Now, the middle text boxes, which are equal, work fine. I can change the first middle one to 6, and the second will be six. Same thing with any other number. But the other two sets of text boxes, the ones that get multiplied by 8, they always come up as NaN.

I give all of the input text boxes a value of 0 when the app is started. Strangely, the dynamic ones were also 0. I tried making the input text boxes start at 2, and sure enough, the dynamic ones were 16. However, they became NaN the second I touched the inputs. Obviously, this means the problem is with the string from the inputs.

I tried tracing the string from one of the inputs before I convert it into an integer. I inputted a 3, and when I traced it, instead of getting "3" like I had expected, I got this: <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="18" COLOR="#000000" LETTERSPACING="0" KERNING="0">3</FONT></P></TEXTFORMAT>

So that's obviously the problem; I just don't know how to fix it so the output is just "3" or whatever the number is.

Here are the variables:
• NTO_x = input that will be multiplied by 8
• NTO_y = input that will not be changed
• NTO_z = input that will be multiplied by 8
• OFN_x = output of NTO_x
• OFN_y = output of NTO_y
• OFN_z = output of NTO_z

And here's the code:

On the frame of the scene:
//Sets the textboxes to number-only
NTO_x.restrict = "0-9";
NTO_x = "0"
NTO_y.restrict = "0-9";
NTO_y = "0";
NTO_z.restrict = "0-9";
NTO_z = "0";
On the second frame of the "control panel" movieclip:
//Multiplies x and z by 8
_root.OFN_x = Number(_root.NTO_x) * 8;
_root.OFN_y = _root.NTO_y;
_root.OFN_z = Number(_root.NTO_z) * 8;



ArtOfWarfare
Jun 3, 2013, 04:41 PM
I'd be kind of surprised if you got an answer here... I'm not under the impression that the people who frequent this section of the forums are familiar with flash action script.

You might want to ask in the web development section of the forums... although I'm under the impression most Mac users (and by extension, people who frequent any section of MacRumors) dislike Flash.

Maybe I'm totally wrong, but just a hunch. StackOverflow might be a good place to take your questions.

epicnyancat
Jun 3, 2013, 05:05 PM
Thanks, that makes sense, I guess. I was just googling for my answer at first, and while I didn't find it, I found a similar thread here, and remembered that I might have made an account a long time ago.

iSee
Jun 3, 2013, 09:03 PM
Well, I did a fair amount of contact work back in the day using Flash, includling AS2...

I guess NTO_x, NTO_y, etc. are variables that are mapped to text fields, right?
(I forget the term, so "mapped" is not the exact right word, but Flash lets you associate a variable with the value of a text field, and that's what those are, right?)

There's a checkbox (in the Flash) for text fields that indicate whether or not they are HTML text fields. You could try unchecking that for the source text fields. Then, I think, the variable associate with the value of the text fields would be a simple stromg like "3".

(You probably don't need them to be HTML text fields... without the HTML option all the text in the text field has to be styled the same way, but that should be OK -- all you have in there is a simple number.)

Alternatively, you could associate the text field objects themselves with a variable. Then, if you used the variable name myHtmlTextField, e.g., you could use myHtmlTextField.text to access the number (as a string, but the string would be just "3" so it would convert directly to a number).

epicnyancat
Jun 3, 2013, 09:41 PM
Well, I did a fair amount of contact work back in the day using Flash, includling AS2...

I guess NTO_x, NTO_y, etc. are variables that are mapped to text fields, right?
(I forget the term, so "mapped" is not the exact right word, but Flash lets you associate a variable with the value of a text field, and that's what those are, right?)

There's a checkbox (in the Flash) for text fields that indicate whether or not they are HTML text fields. You could try unchecking that for the source text fields. Then, I think, the variable associate with the value of the text fields would be a simple stromg like "3".

(You probably don't need them to be HTML text fields... without the HTML option all the text in the text field has to be styled the same way, but that should be OK -- all you have in there is a simple number.)

Alternatively, you could associate the text field objects themselves with a variable. Then, if you used the variable name myHtmlTextField, e.g., you could use myHtmlTextField.text to access the number (as a string, but the string would be just "3" so it would convert directly to a number).
Well, the strange thing is that the HTML checkbox is (and has been) unchecked. But it's still coming out like that.

For your second suggestion, it still comes up as NaN, but tracing it just gives me "undefined".

ytk
Jun 4, 2013, 12:14 PM
Don't know much about ActionScript, but for a quick fix it seems you could just strip out all the tags from the string with a regexp. Here's an example (http://snipplr.com/view/28362/). That's for AS3 though. I guess AS2 doesn't have regexp support built in? So here's an example of how to do it without using a regexp (http://snipplr.com/view/28329/).

epicnyancat
Jun 4, 2013, 04:19 PM
Don't know much about ActionScript, but for a quick fix it seems you could just strip out all the tags from the string with a regexp. Here's an example (http://snipplr.com/view/28362/). That's for AS3 though. I guess AS2 doesn't have regexp support built in? So here's an example of how to do it without using a regexp (http://snipplr.com/view/28329/).
This worked! Thank you so much! I am forever in your debt.
Although one thing I noticed, which has nothing to do with your solution, is that AS2 apparently isn't very good at math. If I put a 0 before a number, it gets screwed up.

Ex: AS2 says that 33 * 1 = 33, but 033 * 1 = 27.
What the hell?

I guess I'll try to make a function that removes any zeros that don't affect the number.

iSee
Jun 4, 2013, 04:36 PM
Well, the strange thing is that the HTML checkbox is (and has been) unchecked. But it's still coming out like that.

For your second suggestion, it still comes up as NaN, but tracing it just gives me "undefined".

Did you try this (It sounds like you might have, but I just want to be sure):
Change the code to (the new stuff is in bold):

NTO_x.restrict = "0-9";
NTO_x.text = "0"
NTO_y.restrict = "0-9";
NTO_y.text = "0";
NTO_z.restrict = "0-9";
NTO_z.text = "0";


and


_root.OFN_x.text = Number(_root.NTO_x.text) * 8;
_root.OFN_y.text = _root.NTO_y.text;
_root.OFN_z.text = Number(_root.NTO_z.text) * 8;

ArtOfWarfare
Jun 4, 2013, 05:06 PM
This worked! Thank you so much! I am forever in your debt.
Although one thing I noticed, which has nothing to do with your solution, is that AS2 apparently isn't very good at math. If I put a 0 before a number, it gets screwed up.

Ex: AS2 says that 33 * 1 = 33, but 033 * 1 = 27.
What the hell?

I guess I'll try to make a function that removes any zeros that don't affect the number.

Prefixing a number with a 0 and without 8s or 9s probably caused it to be recognized as an octal (base 8) number. 3 * 8 = 24 + 3 = 27, so, yes, 33 (base 8) * 1 = 27 (base 10)

epicnyancat
Jun 4, 2013, 05:25 PM
Did you try this (It sounds like you might have, but I just want to be sure):
Change the code to (the new stuff is in bold):

NTO_x.restrict = "0-9";
NTO_x.text = "0"
NTO_y.restrict = "0-9";
NTO_y.text = "0";
NTO_z.restrict = "0-9";
NTO_z.text = "0";


and


_root.OFN_x.text = Number(_root.NTO_x.text) * 8;
_root.OFN_y.text = _root.NTO_y.text;
_root.OFN_z.text = Number(_root.NTO_z.text) * 8;

Yeah, I had tried that before, and I forget exactly what happened, but it definitely didn't work.

epicnyancat
Jun 4, 2013, 05:38 PM
By the way, to anyone who has that problem:
Number("033") = 27
Here's my fix. There's probably a way to make it less complex, but it works just fine:
function stripZeros($myString:String):String
{
if (($myString.substring(0,1) == 0) && ($myString.length != 1)) {
$myString = $myString.substring(1,$myString.length);
if (($myString.substring(0,1) == 0) && ($myString.length != 1)) {
$myString = $myString.substring(1,$myString.length);
if (($myString.substring(0,1) == 0) && ($myString.length != 1)) {
$myString = $myString.substring(1,$myString.length);
if (($myString.substring(0, 1) == 0) && ($myString.length != 1)) {
$myString = $myString.substring(1,$myString.length);
return($myString);
} else {
return($myString);
}
} else {
return($myString);
}
} else {
return($myString);
}
} else {
return($myString);
}
}
Note that this only provides for a maximum of five digits (since that's the character limit on my textboxes).

ytk
Jun 4, 2013, 06:12 PM
That approach may or may not be the best way to accomplish the task, but it would be very easy to rewrite that as a recursive function (and you should do so, if for no other reason than your own education). Think about what you're doing here—you're just applying the same logic to increasingly smaller subsets of the string.

As far as I can tell, you should only need to change a single line of code very slightly (and delete a bunch of redundant lines) to make this function recursive. Basically, it boils down to this: If the test is true, return stripZeros(string_subset). If it's false, simply return the string.

epicnyancat
Jun 4, 2013, 06:55 PM
That approach may or may not be the best way to accomplish the task, but it would be very easy to rewrite that as a recursive function (and you should do so, if for no other reason than your own education). Think about what you're doing here—you're just applying the same logic to increasingly smaller subsets of the string.

As far as I can tell, you should only need to change a single line of code very slightly (and delete a bunch of redundant lines) to make this function recursive. Basically, it boils down to this: If the test is true, return stripZeros(string_subset). If it's false, simply return the string.
Thanks, I was sure that there was a way to do that, but I guess I wasn't thinking logically at the time.

So here's the new function:
function stripZeros($myString:String):String
{
if (($myString.substring(0,1) == 0) && ($myString.length != 1)) {
$myString = $myString.substring(1,$myString.length);
return(stripZeros($myString));
} else {
return($myString);
}
}