2 Replies Latest reply on Jun 16, 2006 11:45 AM by SincityViper

    Function Running before variables have loaded

    SincityViper
      I am running into a problem where the functions within a movie are not waiting for the variables to be pulled from my service. Therefore I get an undefined error on my text box where the time(result of my function) should be displayed.
      Thank you in advance for your time.
      I posted this in Flash Remoting but I think it belongs here. i will post the answer on in the other topic once i recieve it.


      Here is my code.
      Within the root (frame 1) I have the following code:

      import mx.remoting.Service;
      import mx.remoting.PendingCall;
      import mx.rpc.RelayResponder;
      import mx.rpc.ResultEvent;
      import mx.rpc.FaultEvent;

      // create RelayResponder to specify result handling methods
      var myResponder:RelayResponder = new RelayResponder(this,"GetNISTTime_Result","GetNISTTime_Fault");

      // establish gateway and create a Service object to refer to the service
      var ptService:Service = new Service(
      " http://192.168.2.224/flashservices/gateway",
      null,
      "ptFlashComService.adapter",
      null,
      myResponder );

      _root.newclock.clk_loaded = "false";
      _root.newclock.clk_hours = 2;
      trace("not defined yet");

      // call service function
      var temp_pcclock:PendingCall = ptService.GetNISTTime();
      // GetNISTTime_Result and GetNISTTime_Fault are result/fault handlers
      function GetNISTTime_Result (re:ResultEvent):Void {
      trace("Pull success - " + re.result.SUCCESS);
      _root.newclock.clk_success = re.result.SUCCESS;
      _root.newclock.clk_hours = re.result.HOURS;
      _root.newclock.clk_mins = re.result.MINS;
      _root.newclock.clk_secs = re.result.SECS;
      _root.newclock.clk_dayof = re.result.DAYOF;
      _root.newclock.clk_mon = re.result.MON;
      _root.newclock.clk_date = re.result.DATE;
      _root.newclock.clk_loaded = "true";
      trace("is defined");
      }
      function GetNISTTime_Fault (fe:FaultEvent):Void {
      trace("NISTTimeCallFailed-" + fe.fault.description);
      }


      Then I have a movie called newclock that contains 3 frames and a text box that will display the time.
      Frame 1

      //constructor for clock object
      function Clock(h, m, s, ms)
      {
      this.hours = h;
      this.minutes = m;
      this.seconds = s;
      this.millisecs = ms;



      this.endTimer = getTimer()+(ms+s*1000+m*60000+h*3600000);
      this.startTimer = -this.endTimer;

      //explicitly set clock to a certain time
      this.setTime = function (h, m, s, ms)
      {
      this.hours = h;
      this.minutes = m;
      this.seconds = s;
      this.millisecs = ms;

      this.endTimer = getTimer()+(ms+s*1000+m*60000+h*3600000);
      this.startTimer = -endTimer;
      }

      //update clock object with the current time
      //direction: forward = true, backwards = false
      this.updateTime = function(direction)
      {
      millisecs = (direction ? getTimer()-this.startTimer : this.endTimer-getTimer());

      this.hours = (millisecs-(millisecs=(millisecs%3600000)))/3600000;
      this.minutes = (millisecs-(millisecs=(millisecs%60000)))/60000;
      this.seconds = (millisecs-(millisecs=(millisecs%1000)))/1000;
      this.millisecs = substring(millisecs, 1, 2);
      }

      //returns the clock object as a formatted string
      this.displayTime = function()
      {
      hours = this.hours%13; // Allows time to reach the 13th hour, if not clock rest at 12:00:00
      minutes = (this.minutes<10 ? "0" : "")+this.minutes;
      seconds = (this.seconds<10 ? "0" : "")+this.seconds;
      millisecs = (this.millisecs<10 ? "0" : "")+this.millisecs;
      // Forces the clock upon reset to change hour to start at 1
      if (hours == 0) {hours=1;}
      return hours+":"+minutes+":"+seconds; //+"."+millisecs;
      }

      //add to the time on the clock
      this.addTime = function(h, m, s, ms)
      {
      this.endTimer += (ms+s*1000+m*60000+h*3600000);
      this.startTimer -= (ms+s*1000+m*60000+h*3600000);

      updateTime(Clock, true);
      }

      //subtract from the time on the clock
      this.subtractTime = function(h, m, s, ms)
      {
      this.addTime(-h, -m, -s, -ms);
      }
      }



      _root.newclock.standardClock = new Clock(clk_hours,clk_mins,clk_secs,0);



      Frame 2

      //update times
      standardClock.updateTime(true) //true to show passage of time


      //display times
      //trace("Standard: "+standardClock.displayTime())
      var newclock = standardClock.displayTime();


      Frame 3
      gotoAndPlay(3);

        • 1. Re: Function Running before variables have loaded
          SincityViper Level 1
          Can someone please help, I am new to actionscript and I am sure this is a easy fix for someone who knows actionscript.

          Thank you
          Johnny
          • 2. Re: Function Running before variables have loaded
            SincityViper Level 1
            To solve this I did the following.
            I added newclock.gotoAndPlay(3); to my results function.
            function GetNISTTime_Result (re:ResultEvent):Void {
            trace("Pull success - " + re.result.SUCCESS);
            _root.newclock.clk_success = re.result.SUCCESS;
            _root.newclock.clk_hours = re.result.HOURS;
            _root.newclock.clk_mins = re.result.MINS;
            _root.newclock.clk_secs = re.result.SECS;
            _root.newclock.clk_dayof = re.result.DAYOF;
            _root.newclock.clk_mon = re.result.MON;
            _root.newclock.clk_date = re.result.DATE;
            newclock.gotoAndPlay(3);
            }

            Then within my clock movie I added three additional frames at the beginning.
            The first frame as stop();.
            Frame 2 is blank
            The third frame I added if (clk_success == "false") {
            gotoAndPlay(2);}


            Now the movie will loop frames 2 and 3 until the variables are passed.
            Plus, I cleaned up a few lines of code for that were there only for testing.
            This may not be the best fix but it works. Does anybody have a better way of doing it?

            Johnny