Friday, April 2, 2010

NaN

Hello mates

In Action Script 2,

When I type any maths formula (subtraction / tan / ^2) I always get the result NaN (Not a Number)

How can I resolve it?

e.g on(release) = {south - west}

and I enter number 5 in south and 6 in west.

I get the result, NaN.

same when I try on(release) {result = Math.atan2(Fy,Fx)}

(I already named the input boxes as Fy Fx and result)

but I get the answer in dynamic box NaN.

How can I resolve that, and get number instead of Tan?

thx alot.

NaN

Hi,

Try this piece of code:

on(release) {

result = Math.atan2(Number(_root.Fy.text),Number(_root.Fx.text));

trace(result);

}

Saransoft

NaN

As the previous response to your posting shows, when you are usng textfields, to get the content from them you need to access the text property of them... Fy.text, Fx.text... etc?And when you use those values in equations, it is safest to use the Number() function to convert them from string to number values... sometimes not doing so will still workin AS2, but it is safest to be sure and to get into the practice of doing because in AS3 it doesn't.

Actually the safest way is to use parseInt() or parseFloat() those are the actual functions that convert text to numbers.

Casting as a Number() doesn't actually convert the value and there are (rare) times when I have seen it fail.

hey mate

Thx for answering me, But I am afraid it didn't work.

Original script was. On(release) {NS = North - South}

After u said this, I wrote: on(release) {NS = parseFloat(North) - parseFloat(south)}

But it still give NaN. So, mind giving me a written script? I am afraid I made a mistake or so.

Thank you alot.

hey mate

Thanks for helping me, but I am still beginner in flash, not expert in writing codes.

All I want, I have 4 input boxes, North, East, South and West.

I wanna subtract North - South and East - West.

tried on(release) { NS = North - South}

But I got in the dynamic box NaN.

Can you write me the full code plz?

I really appreciate it.

Thanks alot

hey mate

Thanks for helping me, but I am still beginner in flash, not expert in writing codes.

All I want, I have 4 input boxes, North, East, South and West.

I wanna subtract North - South and East - West.

tried on(release) { NS = North - South}

But I got in the dynamic box NaN.

Can you write me the full code plz?

I really appreciate it.

Thanks alot

the previous posts here have given you the answer ?- when attempting to access the value within the TextField, you must target the 'text' property of the field - you are closest in the response to RRs post using parseFloat - however you did not target the 'text' property, so ...

on(release) {

?var NS = parseFloat(North.text) - parseFloat(South.text);

}

OR

on(release) {

?var NS = Math.atan2(parseFloat(Fy.text), parseFloat(Fx.text));

}

REM: atan2 will return a value in Radians

also - it 'may' be necessary to target _root or _parent as used above as well - it will depend on your structure.

additionally - it is a 'best practice' *not* to use 'Object attachment' methods (eg. the placement of code 'on' a MC object - using the on() handler in this manner)?instead one should place all code on the timeline or within class files - if placed on the timeline the correct code would be:

var NS:Number;

[btnInstanceName].onRelease = function() {

?NS = parseFloat(North.text) - parseFloat(South.text);

}

hey matey

I tried everyone of ur scripts, still getting NaN.

Can u make a flash document, insert 4 input text boxes and 2 dynamic, and write the scripts, on either button or timeline, and send me it (*fla)?

cuz IDK whats problem with my flash.

plz after u make it send to Car_orange@hotmail.com

or upload it to www.4shared.com and put link here.

I appreciate it.

thx alot

A couple more things to check. Look to see if the auto kern box is checked on your text fields. Make sure it is off.

Also add the following trace statements:

trace(''South is: [''+South.text+'']);

trace(''South parsed is: ''+parseFloat(South.text,10));

What do those trace?

hey mate

I checked the auto Kern, and yea, it was on, so I changed it to off.

And about the second thing, after I typed it I got:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: String literal was not properly terminated
trace(''South is: [''+South.text+'']);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: ')' or ',' expected
trace(''South parsed is: ''+parseFloat(South.text,10));

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement must appear within on handler
trace(''South is: [''+South.text+'']);

Total ActionScript Errors: 3?Reported Errors: 3

thanks alot.

You're missing onew quotation mark at the end...

trace(''South is: [''+South.text+'']'');

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Statement must appear within on handler
trace(''South is: [''+South.text+'']'');

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Statement must appear within on handler
trace(''South parsed is: ''+parseFloat(South.text,10));

Total ActionScript Errors: 2?Reported Errors: 2

Yea mate, thx alot, actually 1 problem solved, still got 3 to go...

Can you write me the full code?

on(release) {NS = North - South}

thats what I know, can u continue the rest about tracing and so?

thx alot.

those 'trace' statements need to be placed within the on(release) handler

hey matey

I wrote:

on(release) {NS = Number(North) - Number(south)
trace(''South is: [''+South.text+'']'');

trace(''South parsed is: ''+parseFloat(South.text,10));
}

But I got NaN, and this:

South is: [undefined]
South parsed is: NaN

So how cna we resolve this?

thx alot.

aww mates, no answer?

I really need help with that %26gt;_%26lt;

thx anyway

NS = Number(North) - Number(south)

Well that part isn't going to work, because you aren't accessing the text properties.

This trace(''South is: [''+South.text+'']''); is telling you that there is no South.text property. Capitalization matters. Does this code match what is on your stage?

What if you add a trace like this:

trace(''Current scope is: ''+this);

trace(''Found south: ''+south);

What do you get for those?

The choices pretty much are:

There is a spelling or capitalization error.

The input field south (or South) does not exist at all when the button is pressed.

The input field is in some other timeline that is not being properly referenced.

There is some other local variable named south that exists, but is undefined.

So we just have to figure out which of those it is. You can also try the same traces with north or North or what ever it is. Are they the same?

Its ok mate.

I was getting Nan, cuz we were only tracing South, I tried to trace north, too, and it worked. Now no NaN anymore. You gonna get the 10 points for correct answer, and everyone tried to answer is thanked for faithful help, he will get 5 points for helpful answer.

1 more thing, I need.

TF = (NS)^2 x (EW)^2

how can we write that code? (TF is the result)

  • eye shadow
  • No comments:

    Post a Comment