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.

Community
  • 1
  • 1
Carson
  • 1
  • 2
  • 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? – Panzercrisis Apr 30 '15 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? – BadFeelingAboutThis Apr 30 '15 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. – Allan May 01 '15 at 00: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. – Carson May 02 '15 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. – BadFeelingAboutThis May 04 '15 at 18:07
  • @LDMS I am completely not sure of that, the only code in my program is the code you see above. My variables are not defined until the user inputs text (right?) so how do I make them accessible to my `btnAdd.onRelease` function? – Carson May 04 '15 at 22:10
  • can you share your .fla? That would be easiest for figuring this out. – BadFeelingAboutThis May 04 '15 at 22:26
  • @LDMS Of course, here is a link to the download: https://www.mediafire.com/?c2ogiakjfv50cej – Carson May 04 '15 at 22:30

1 Answers1

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);
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • 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. – Carson May 05 '15 at 01:28
  • Yes, instead of accessing the variable assigned to the text field, you directly access the textfield objects `.text` property. – BadFeelingAboutThis May 05 '15 at 16:00