PDA

View Full Version : calculation problem!


JazzB
08-13-2009, 07:31 AM
Hi all:

I'm new here.

I've some little strange questions about my calculation exercise.

enter_btn.onRelease = function() {
outputNum = inputNum * 2;
trace("the inputNum data value type is " + typeof(inputNum));
trace("value: " + inputNum);
trace("the outputNum data value type is " + typeof(outputNum));
trace("value: " + outputNum);
};
var inputNum:Number = 0;
var outputNum:Number = 0;
trace(typeof (inputNum));
trace(typeof (inputNum));

I,ve no idea what to do with it.
The types of both inputNum and outputNum variables are Numbers when I publish the movie, but it shows me the types of both are immediately changed to Strings instead of Numbers, or NaN when I enter numbers and then press the enter button.

I declared both variables Numbers. But why? I really have no idea.
I've also tried the it on Flash CS4 with AS2.0 and Flash 8, but both show me the same error.

However, when I do the calculation within the trace command, it works properly with no errors. how strange...

Any idea and why?
Please help me.
Thanks in advance.:)

JazzB

raydowe
08-13-2009, 02:22 PM
you are using inputNum and outputNum before you declare them as variables. I'm guessing what's happening is that they are being used as strings inside the function because they are tied to an input text field.

Switch your code to:

var inputNum:Number = 0;
var outputNum:Number = 0;
trace(typeof (inputNum));
trace(typeof (inputNum));

enter_btn.onRelease = function() {
outputNum = inputNum * 2;
trace("the inputNum data value type is " + typeof(inputNum));
trace("value: " + inputNum);
trace("the outputNum data value type is " + typeof(outputNum));
trace("value: " + outputNum);
};


and it says it's a number.

Althought there seems to be some HTML formatting going on that also gets caught as being the value of the text field. Did you write code somewhere for this or is it being done automatically on the field?

JazzB
08-14-2009, 04:21 AM
Thanks, raydowe!
tried it and still doesn't work.

That's right. inputNum and outputNum are tied to an input text field. They can't be strings because I've declared them as numbers. As you said, the output panel shows some HTML formatting...

I created two text fields, one for input and one for dynamic, and each field has a variable inside property inspector , inputNum and outputNum respectively. That's what I did.

var inputNum:Number = 0;
var outputNum:Number = 0;
trace(typeof (inputNum));
trace(typeof (inputNum));

enter_btn.onRelease = function() {
outputNum = inputNum * 2;
trace("the inputNum data value type is " + typeof(inputNum));
trace("value: " + inputNum);
trace("the outputNum data value type is " + typeof(outputNum));
trace("value: " + outputNum);
};

Let me explain in detail:

When publishing, the output panel shows me inputNum and outputNum as numbers.

Then, just pressing the Enter button with the default 0 values, it also says numbers.

Next, try changing the value of inputNum from 0 to, for example, 2 within the actions panel, not on the swf. The outputNum value will be 4, which is a number.

So far, everything works perfectly.

Now, try typing some other numbers and press the enter button, the output panel says string for inputNum with some HTML formatting stuff.

I've also tried Number(inputNum); and Number(outputNum); within the event handler. And still doesn't work...

I just want to know, if it is just my case or everyone gets the same result.

I've no idea how to deal with it. What's wrong with the code or bugs in flash:confused:

Thanks again
JazzB

BronL
08-18-2009, 07:10 AM
Hiya

I think you may be missing understanding that the contents of a textfield will always be a string. I'm guessing you have a variable attached to a textfield?

In your enter_btn.onRelease, try

outputNum = Number(inputNum) * 2;

If that doesn't work, how about uploading a zip of your fla for us to have a look? [edit - so read the entire thread, BronL]

cheers

BronL
08-19-2009, 01:09 AM
Hi

A text field takes on the value of a variable that is assigned to it, not the other way around.

Have a look at this. Note that I have given the fields instance names so we could get at their contents. In this, we assign the value of input_fld's text to the outputnum variable, which then updates the contents of the output_fld.

HTH