2 Replies Latest reply on Dec 20, 2011 3:47 PM by Daniel Gimness

    Profit and Loss calculator

    Daniel Gimness Level 1

      Hello,

       

      I'm trying to get a Profit and Loss calculator working in ActionScript 2. See image below:

       

      P_L calculator.jpg

       

       

      The red text in the yellow highlighted cells is what the user can change. They click a calculate button and the number of total value for lemons * Unit cost is supposed to be updated.

      Below is the code that I have on the calculate button.

       

      --------------------------------------------------------

       

      on (release) {

                trace("numLemons="+numLemons);

                trace("lemonCost="+lemonCost);

                trace("lemonTotal="+lemonTotal);

       

                lemonCostDynamic=lemonCost;

                lemonTotal = numLemons * lemonCostDynamic;

                iceTotal = numIce * iceCost;

                sugarTotal = numSugar * sugarCost;

                cupsTotal = numCups * cupsTotal;

      }

       

      --------------------------------------------------------

       

      When the calculate button is pressed, I get the NaN values below:

       

      P_L calculator_error.jpg

       

      Please, what I am missing?

       

      Thanks,

       

      Dan

        • 1. Re: Profit and Loss calculator
          Ned Murphy Adobe Community Professional & MVP

          There could be a couple of things working against you.  One could be that the numeric values you see are actually string values, so you might need to force them to be numbers in your calculations....

           

                    lemonCostDynamic=Number(lemonCost);

                    lemonTotal = Number(numLemons) * Number(lemonCostDynamic);

                    ... and so on

           

          Another thing could be you are using the var aspect of a textfield get the value in the textfield which in my own experience (long ago) was always problematic.  The cure for this is to not use the var aspect, assign the textfields an instance name, and use the text property of the textfield to get the values...

           

             lemonCostDynamic=Number(lemonCost.text); // where lemonCost is now the instance name of the textfield, not a var assigned to it

             lemonTotal.text = String( Number(numLemons.text) * Number(lemonCostDynamic) );

           

          Just try resolving the first problematic result and that solution should work for the remaining ones as well.

           

          One other thing to get away from doing is assigning any code on() objects.  Instead, assign an instance name to your button and place the code for it in the timeline.  That way there will be less of any issue when it comes to targeting things. If you named your button "calc_btn", then you would code it in the timeline using...

           

          calc_btn.onRelease = function(){

                    lemonCostDynamic=...

                    ... andso on

          }

          1 person found this helpful
          • 2. Re: Profit and Loss calculator
            Daniel Gimness Level 1

            Thanks Ned! I will try what you suggested and let you know the results...

             

            Dan