Multiplication of variables...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Germaris
    New Member
    • Jan 2008
    • 18

    Multiplication of variables...

    Hi there!

    Here is the whole script of a test document which doesn't work...
    Excuse me but I never used multiplication of variables and I confess how shameful I feel.
    I absolutely don't understand the explanations the Flash Help contains...

    Document contains three Input Fields, a Dynamic Field and a Button.
    Clicking the Button returns NaN !!!

    I thank you in advance for any help.

    Best regards from Old Gerry



    var a:Number = new Number(field1.t ext);
    var b:Number = new Number(field2.t ext);
    var c:Number = new Number(field3.t ext);
    operationFct = function () {
    var res:Number = new Number();
    res = (a * b) + c;
    resultFld.text = res;
    };
    myButton.onRele ase = function() {
    operationFct();
    };
    stop();
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    here is an example:
    I assuming you have 3 input text boxes and one Dynamtic text box and you gave them Variable names. I will use
    Code:
    number_one 
    number_two 
    number_three
    result_sum

    Limit the textboxes to numbers only. To do this, just select a textbox and click the Character… button in the Properties panel. This will bring up the Character Options box. From there, select “Only”, “Numerals (0-9)”, and click Done.

    Now, we need to add our button used to execute our calculations. In the Component panel, select Flash UI Components.
    Drag and drop the PushButton component into the scene

    Select the PushButton component you’ve just dragged into your scene. In the Properties panel, label the component “Calculate” and set the Click Handler to “onCalculate”. This is the button we will use to calculate the user’s data.


    As a final measure, I have given the “Number 1” and “Number 2” textboxes a value of zero.

    it’s time to put in the code that will make this movie work.

    Add a new layer to the movie, and call it “actions”.

    Select the first frame of the “actions” layer. In the Actions panel, add the following code:

    Code:
    function onCalculate() { 
     one = Number(number_one); 
     two = Number(number_two);
     three =  Number(number_three);
     result_sum = (one * two) + three; 
     
    }
    This is the code that will make this movie do its magic! Let’s look at what this code does and why.

    Code:
    function onCalculate() {
    This first line starts a new function in the movie. Remember we gave PushButton component a Click Handler of “onCalculate”. When this button is clicked, it will execute the code within this function.

    Code:
    one = Number(number_one); 
    two = Number(number_two);
    three =  Number(number_three);
    This code has two purposes. The first is to give shorter variable names to the data that’s being calculated. Instead of spelling out “number_one” throughout our code, we can now just use “one”.

    We do this to treat these variables as numbers. This is done with Number(), which tells Flash that we to treat values in parentheses as numbers. If we don’t, when we calculate 1 plus 1, we’ll get 11. Or, if we calculate 1 plus 2, we’ll get 12. Instead, with the Number(), when we calculate 1 plus 1, we’ll get 2.

    Code:
    result_sum = (one * two) + three;
    }

    Remember the variable names we gave the textboxe at the bottom? This code puts our calculation results within that textbox. When this code is executed, the “result_sum” textbox will display the result of mutipling Number 1 and Number 2, then adds Number 3. The “result_product ” textbox will display the result .

    That’s it! Publish your movie, type in some numbers in first two textboxes, and hit Calculate.

    nomad.
    PS I hope this works I really did not test it.
    Last edited by nomad; Feb 27 '08, 10:47 PM. Reason: add code markers

    Comment

    • Germaris
      New Member
      • Jan 2008
      • 18

      #3
      Hey, nomad, it's you here again!

      Thank you, Mr. Professor for this full explanation of the whys it is working or not.
      It should take a lot of time to respond so completely...
      After reading your post, I feel a little more "intelligen t" and younger !
      :-)

      Oh, by the way: IT WORKS !!!

      Have a very nice Thursday.
      Best regards,

      Gerry

      Comment

      Working...