3 Replies Latest reply on Nov 18, 2008 2:14 PM by Rothrock

    Math min for 0?

    shintashi Level 1
      this is my math string, but I don't want it to go below zero when object mass exceeds the total. Is there a way to Math.min this or something so the string caps at 0? I want to avoid creating an outside statement about if object_mass < 0 {object_mass = 0} (not to mention i have no idea if that would work with this kind of statement)

      field_strength.text = ((Number(psi.text) - Number(anti_psi.text))*(2 + Number(psi.text) - Number(anti_psi.text))) - Number(object_mass.text);

      thanks.
        • 1. Re: Math min for 0?
          Rothrock Level 5
          Flash supports ternary statements you can do something like:

          field.text= (somVar>0) ? trueValue : falseValue;

          However with a long calculation like yours it gets hard to read. Generall in situations like that I use an intermediate variable.

          var temp=someLong + Calcuation

          var myVar= (temp>=0) ? temp : 0;
          • 2. Re: Math min for 0?
            shintashi Level 1
            Rothrock, could you explain that in English? I am an amateur programmer.

            Flash supports ternary statements (three component statements?) you can do something like:

            field.text= (somVar>0) ? trueValue : falseValue;

            --> is somVar "some Variable" ?



            var temp=someLong + Calculation
            var myVar= (temp>=0) ? temp : 0;


            ---> are you saying put the equation in the form of a variable and then put that variable inside a second variable that contains the (if less than 0 = 0)? if I do this, How do I stick it back in my text string for display?

            ---- in other words, does this... -----

            psi.onChanged = function(textfield_txt:TextField) {
            if (psi.text != "") {
            psi_aura.text = Number(psi.text) * (2 + Number(psi.text));
            field_strength.text = ((Number(psi.text) - Number(anti_psi.text))*(2 + Number(psi.text) - Number(anti_psi.text))) - Number(object_mass.text);
            }
            };

            ---- become something like this .... ? ------

            var mantra=((Number(psi.text) - Number(anti_psi.text))*(2 + Number(psi.text) - Number(anti_psi.text))) - Number(object_mass.text);
            var sutra= (mantra>=0) ? temp : 0;


            psi.onChanged = function(textfield_txt:TextField) {
            if (psi.text != "") {
            psi_aura.text = Number(psi.text) * (2 + Number(psi.text));
            field_strength.text = sutra;
            }
            };

            ------ thanks ----

            p.s.

            substituted temp for mantra, myVar for sutra in:
            var temp=someLong + Calculation
            var myVar= (temp>=0) ? temp : 0;


            Do I need to keep the second semi colon ";" - i.e. will the program read "sutra;" as "code blah blah blah;;" or as "code blah blah blah;"
            • 3. Re: Math min for 0?
              Rothrock Level 5
              Yes. The part between the parens is evaluated and if true then the value before the colon is assigned and if false the value after the colon.

              Usually the part in the parens involves a variable of some sort. You could do this:

              currentX = (true) ? 10 : 0;

              But (true) will always be true so currentX will always be 10, so whats the point? :)

              Well I'm not quite clear on what all you are doing, but I think Something like this:

              psi.onChanged = function(textfield_txt:TextField) {
              if (psi.text != "") {
              psi_aura.text = Number(psi.text) * (2 + Number(psi.text));

              var mantra=((Number(psi.text) - Number(anti_psi.text))*(2 + Number(psi.text) - Number(anti_psi.text))) - Number(object_mass.text);

              field_strength.text = (mantra > 0) ? matra : 0;
              }
              };

              That says on the change of psi if psi isn't null (you will need to do something if it is null!) then calculate mantra and if mantra is greater than zero put it in field_strength textfield if mantra is thess than zero then just give field_strength a zero.

              Again. I'm not sure exactly all of which bits of your calculation need to go where and when. But I hope you are getting the idea.

              Also I don't know which semicolon you are refering to.