1 Reply Latest reply on Apr 25, 2011 7:01 AM by Ned Murphy

    How to pass values from one function to another

    ohtecktiong

      Hi,

       

      I am a middle school teacher and a newbie in Flash Actionscript. I am trying to create a countdown timer for use in my class during tests. The start and pause functions work as required, but not the pause button. When I click on the pause button, the timer is reset to 0:00:00. Please help. Here is the code I had written so far:

       

      var Subject1timeLeftHr;

      var Subject1timeLeftMin;

      var Subject1timeLeftSec;

       

      Subject1start_btn._visible = true;

      Subject1pause_btn._visible = false;

      Subject1rotor_mc.gotoAndStop(1);

      Subject1rotor_mc._visible = false;

      Subject1durationHr_txt.text = "0";

      Subject1durationMin_txt.text = "00";

      Subject1durationSec_txt.text = "00";

       

      Selection.setFocus(Subject1durationHr_txt);

      function SubjectdurationHr(SubjectxdurationHr_txt, SubjectxdurationMin_txt)

      {

      if (SubjectxdurationHr_txt.length == 1)

      {

      Selection.setFocus(SubjectxdurationMin_txt);

      }

      }

      function SubjectdurationMin(SubjectxdurationMin_txt, SubjectxdurationSec_txt)

      {

      if (SubjectxdurationMin_txt.length == 2)

      {

      Selection.setFocus(SubjectxdurationSec_txt);

      }

      }

      function SubjectdurationSec(SubjectxdurationSec_txt, SubjectxdurationHr_txt)

      {

      if (SubjectxdurationSec_txt.length == 2)

      {

      Selection.setFocus(SubjectxdurationHr_txt);

      }

      }

      Subject1durationHr_txt.onChanged = function()

      {

      SubjectdurationHr(Subject1durationHr_txt,Subject1durationMin_txt);

      };

      Subject1durationMin_txt.onChanged = function()

      {

      SubjectdurationMin(Subject1durationMin_txt,Subject1durationSec_txt);

      };

      Subject1durationSec_txt.onChanged = function()

      {

      SubjectdurationSec(Subject1durationSec_txt,Subject1durationHr_txt);

      };

      function startcountdown(SubjectxdurationLeft, SubjectxdurationHr, SubjectxdurationHr_txt, SubjectxdurationMin, SubjectxdurationMin_txt, SubjectxdurationSec, SubjectxdurationSec_txt, Subjectxduration, SubjectxstartTime, SubjectxendTime, Subjectxtimer_mc, Subjectxpause_btn, Subjectxstart_btn, Subjectxrotor_mc, SubjectxtimeLeft, SubjectxtimeLeftHr, SubjectxtimeLeftMin, SubjectxtimeLeftSec, SubjectxtimeLeftHr_txt, SubjectxtimeLeftMin_txt, SubjectxtimeLeftSec_txt)

      {

      delete SubjectxdurationLeft;

      delete SubjectxdurationHr;

      delete SubjectxdurationMin;

      delete SubjectxdurationSec;

      delete Subjectxduration;

      delete SubjectxdurationHr_txt.text;

      delete SubjectxdurationMin_txt.text;

      delete SubjectxdurationSec_txt.text;

      SubjectxstartTime = getTimer();

       

       

      Subjectxtimer_mc.onEnterFrame = function()

      {

      if (SubjectxdurationHr_txt.text == Nan || SubjectxdurationMin_txt.text == Nan || SubjectxdurationSec_txt.text == Nan)

      {

      }

      else

      {

       

       

      SubjectxdurationHr = 60 * 60 * 1000 * Number(SubjectxdurationHr_txt.text);

      SubjectxdurationMin = 60 * 1000 * Number(SubjectxdurationMin_txt.text);

      SubjectxdurationSec = 1000 * Number(SubjectxdurationSec_txt.text);

      Subjectxduration = SubjectxdurationHr + SubjectxdurationMin + SubjectxdurationSec;

      SubjectxendTime = SubjectxstartTime + Subjectxduration;

      SubjectxdurationLeft = SubjectxendTime - getTimer();

      if (SubjectxdurationLeft > 0)

      {

      SubjectxdurationHr_txt._visible = false;

      SubjectxdurationMin_txt._visible = false;

      SubjectxdurationSec_txt._visible = false;

      Subjectxpause_btn._visible = true;

      Subjectxstart_btn._visible = false;

      Subjectxrotor_mc._visible = true;

      Subjectxrotor_mc.play();

      SubjectxtimeLeft = SubjectxdurationLeft / (1000 * 60 * 60);

      SubjectxtimeLeftHr = Math.floor(SubjectxtimeLeft);

      SubjectxtimeLeftMin = Math.floor((SubjectxtimeLeft - SubjectxtimeLeftHr) * 60);

      SubjectxtimeLeftSec = Math.floor(((SubjectxtimeLeft - SubjectxtimeLeftHr) * 60 - SubjectxtimeLeftMin) * 60);

       

       

      SubjectxtimeLeftHr_txt.text = String(SubjectxtimeLeftHr);

      if (SubjectxtimeLeftHr_txt.length < 1)

      {

      SubjectxtimeLeftHr_txt.text = "0" + SubjectxtimeLeftHr_txt.text;

      }

      SubjectxtimeLeftMin_txt.text = String(SubjectxtimeLeftMin);

      if (SubjectxtimeLeftMin_txt.length < 2)

      {

      SubjectxtimeLeftMin_txt.text = "0" + SubjectxtimeLeftMin_txt.text;

      }

      SubjectxtimeLeftSec_txt.text = String(SubjectxtimeLeftSec);

      if (SubjectxtimeLeftSec_txt.length < 2)

      {

      SubjectxtimeLeftSec_txt.text = "0" + SubjectxtimeLeftSec_txt.text;

      }

      }

      else

      {

      delete Subjectxtimer_mc.onEnterFrame;

      SubjectxtimeLeftHr_txt.text = "";

      SubjectxtimeLeftMin_txt.text = "";

      SubjectxtimeLeftSec_txt.text = "";

      SubjectxdurationHr_txt._visible = true;

      SubjectxdurationMin_txt._visible = true;

      SubjectxdurationSec_txt._visible = true;

      Subjectxrotor_mc.gotoAndStop(1);

      Subjectxrotor_mc._visible = false;

      SubjectxdurationHr_txt.text = "0";

      SubjectxdurationMin_txt.text = "00";

      SubjectxdurationSec_txt.text = "00";

      Subjectxpause_btn._visible = false;

      Subjectxstart_btn._visible = true;

      Selection.setFocus(SubjectxdurationHr_txt);

      }

      }

      };

      }

       

      function pausecountdown(SubjectxdurationHr_txt, SubjectxtimeLeftHr, SubjectxdurationMin_txt, SubjectxtimeLeftMin, SubjectxdurationSec_txt, SubjectxtimeLeftSec, Subjectxstart_btn, Subjectxpause_btn, Subjectxrotor_mc)

      {

      delete Subjectxtimer_mc.onEnterFrame;

      SubjectxdurationHr_txt.text = String(SubjectxtimeLeftHr);

      SubjectxdurationMin_txt.text = String(SubjectxtimeLeftMin);

      SubjectxdurationSec_txt.text = String(SubjectxtimeLeftSec);

      Subjectxstart_btn._visible = true;

      Subjectxpause_btn._visible = false;

      Subjectxrotor_mc.stop();

      }

       

      Subject1pause_btn.onRelease = function()

      {

      pausecountdown(Subject1durationHr_txt,Subject1timeLeftHr,Subject1durationMin_txt,Subject1t imeLeftMin,Subject1durationSec_txt,Subject1timeLeftSec,Subject1start_btn,Subject1pause_btn ,Subject1rotor_mc);

       

      };

       

      Subject1start_btn.onRelease = function()

      {

      startcountdown(Subject1durationLeft,Subject1durationHr,Subject1durationHr_txt,Subject1dura tionMin,Subject1durationMin_txt,Subject1durationSec,Subject1durationSec_txt,Subject1durati on,Subject1startTime,Subject1endTime,Subject1timer_mc,Subject1pause_btn,Subject1start_btn, Subject1rotor_mc,Subject1timeLeft,Subject1timeLeftHr,Subject1timeLeftMin,Subject1timeLeftS ec,Subject1timeLeftHr_txt,Subject1timeLeftMin_txt,Subject1timeLeftSec_txt);

      };

       

      Subject1cancel_btn.onRelease = function()

      {

      Subject1timeLeftHr_txt.text = "";

      Subject1timeLeftMin_txt.text = "";

      Subject1timeLeftSec_txt.text = "";

      Subject1durationHr_txt._visible = true;

      Subject1durationMin_txt._visible = true;

      Subject1durationSec_txt._visible = true;

      Subject1durationHr_txt.text = "0";

      Subject1durationMin_txt.text = "00";

      Subject1durationSec_txt.text = "00";

      Subject1timeLeftHr_txt._visible = true;

      Subject1timeLeftMin_txt._visible = true;

      Subject1timeLeftSec_txt._visible = true;

      Subject1pause_btn._visible = false;

      Subject1start_btn._visible = true;

      Subject1rotor_mc._visible = false;

      Subject1rotor_mc.gotoAndStop(1);

      delete Subject1timer_mc.onEnterFrame;

      delete Subject1durationLeft;

      delete Subject1duration;

      delete Subject1durationHr_txt.text;

      delete Subject1durationMin_txt.text;

      delete Subject1durationSec_txt.text;

      };

       

       

        • 1. Re: How to pass values from one function to another
          Ned Murphy Adobe Community Professional & MVP

          I think you need to spend some time reducing your code to practical levels.  You seem to be passing everything in the book to every function and I would guess that probably none of it is necessary.  If you declared those variables at the beginning, then you don't need to pass them into any function because they are gobally available to any of the code/functions that follows them.  Similarly, if you have textfields on the stage, you do not need to pass those into any functions for the same reason.

           

          I see you making overuse of "delete" (and possibly errant use as well).  Probably the only thing you might want/need to use is...

           

          delete Subjectxtimer_mc.onEnterFrame;

           

          Which stops the enterframe activity from firing off, which I will guess is being used to update the textfields that indicate the time.

           

          And that conditional that uses == Nan isn't likely to do anything except wonder what an Nan is.  Textfields hold strings, which are quoted.  SO unless you have a variable named Nan somewhere that has a String value assigned to it, that conditional won't be doing anything for you.  You probably won't need it at all if you get this working properly.