0

I'm pretty new around here, and I've recently bought a book about flash game programming with Actionscript 1.0.

Let me be clear: No threads on this website or others have an answer to my question.

I know you guys have gotten many questions about the "parseInt" function used in Javascript, however none of them provide me with an answer.

I'm doing one of the excercises which involves creating an adder program, the user inputs two numbers and the program adds them together. My code looks like this:

btnAdd.onRelease = function() { sum = parseInt(A) + parseInt(B) }

This is an incredibly simple program which is infuriatingly refusing to work. Every time I test it, the output is NaN. Without fail.

A and B are referring the variables which I assigned to the Input text boxes. "sum" is referring to the variable which I assigned to the text box that displays the Output (Dynamic).

No, my variables are not wrong, I have probably sextuple checked them. The button instance name is correct too.

In my research to find an answer to this stupidly easy question, I've found these places: Research 1 Research 2

The first website has someone with the exact same question, however it was never answered and the one guy who did only suggested alternative coding which also returned "NaN".

There are also many other unrelated sites on stackoverflow that are discussing parseInt in different situations.

The only things I have actually learned from these places is that parseInt only returns NaN if it can't parse the string to an integer i.e. "fsd" would return "NaN" but "4" should return "4".

I enter only integers every time but it always gives me NaN.

I also tested the program without the parseInt like so:

btnAdd.onRelease = function() { sum = A + B }

I know this works perfectly because the output shows the two numbers next to each other (1 + 2 becomes 12, 4 + 6 becomes 46 etc.)

This is obviously due to flash thinking that the numbers are strings, when I really want them to be integers. That's what the parseInt is supposed to do.

What's happening? If you need pictures or any more info let me know.

8
  • How did you find a resource on AS1? I tried looking for it a year or two ago out of curiosity, and it looked like any manuals or compilers were completely buried. Is it maybe AS2? Commented Apr 30, 2015 at 21:52
  • 1
    What are you compiling with? Flash Professional? If so what version. Out of curiosity, why learn AS1? I tried compiling your example to AS1 in FlashPro CS6 without issue. Are you sure A and B are available in the scope of your onRelease? Commented Apr 30, 2015 at 22:34
  • Learn something else. ActionScript 1.0 is super old and is a first gen hacky script for allowing designers back in the day to add some very basic features to their animations. ActionScript 3.0 would be far more relevant, and far closer to a real programming experience, but even then I would look elsewhere. Commented May 1, 2015 at 0:00
  • I am compiling with Adobe Flash Professional CS6, and I'm not sure what you mean by "scope". Could you elaborate? (I'm a newby) I'm learning Actionscript 1 because it is the programming language available in the book I have. I have programmed with AS3 before however. Commented May 2, 2015 at 23:14
  • By scope, I mean you don't show where and how A and B are defined. So are you sure A and B are accessible in the anonymous function you've assigned to btnAdd.onRelease. When replying to comments on Stack sites, use @username , otherwise no one will be notified of your reply. Commented May 4, 2015 at 18:07

1 Answer 1

0

In Flash Professional CS6, publishing to Flash Player 5 with ActionScript 1.0, I have successfully done this:

var A = "1";
var B = "3";

trace(A + B);//outputs 13
sum = parseInt(A) + parseInt(B); 
trace(sum);// outputs 4

This clearly demonstrates that parseInt() functions correctly.


After looking at your .fla, the problem seems to be that linking your input text to variable, stuffs HTML into said variable.

So the value of A after entering in the number 4, is:

<P ALIGN="CENTER"><FONT FACE="Arial" SIZE="40" COLOR="#000000" LETTERSPACING="0" KERNING="0">4</FONT></P>

My guess is this is a bug with Flash CS6 (as it's treating the input text fields like HTML text, even with html text turned off) - doesn't seem to be a problem with dynamic text fields.

Here is what I did to get it working:

  1. Gave your first text field an instance name of txtA.
  2. Gave your second text field an instance name of txtB.

enter image description here Changed the code to the following:

btnAdd.onRelease = function() {
    sum = parseInt(txtA.text) + parseInt(txtB.text);
}
Sign up to request clarification or add additional context in comments.

2 Comments

So to confirm, the fix for this was to convert the HTML to text, and then use parseInt on the instance name instead of the variable? Thank you by the way, your code works perfectly.
Yes, instead of accessing the variable assigned to the text field, you directly access the textfield objects .text property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.