6 Replies Latest reply on Jul 22, 2009 11:18 PM by kglad

    Can't get getBytesLoaded and getBytesTotal to work properly

    jshrek

      I am using Flash CS3 with ActionScript 2 and am having a problem getting getBytesLoaded and getBytesTotal to work properly. Probably I am taking the reading in the wrong spot, but I can't figure out where I should be doing it. The code will read an xml file which contains pictures and url links to where I want each picture to go, and then loads and shows a picture for 3 seconds, then goes on to the next one. I am trying to show the preloader bar "LOADING" by adjusting the _xscale as a percentage of BytesLoaded/BytesTotal. The code should work, but my getBytesLoaded and getBytesTotal is either showing 0% or 100% and nothing in between. Here is my code and I have left some of my trace statements in the code below as well. The problem occurs in the this.onEnterFrame = function() which is about half way thru the code:

       

      function loadXML(loaded) {
          if (loaded) {
              xmlNode = this.firstChild;
              image = [];
              linkto = [];
              description = [];
              total = xmlNode.childNodes.length;
              for (i=0; i<total; i++) {
                  image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
                  linkto[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
                  description[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
              }
              firstImage();
          } else {
              content = "error: xml file not loaded!";
          }
      }
      xmlData = new XML();
      xmlData.ignoreWhite = true;
      xmlData.onLoad = loadXML;
      xmlData.load("slideshow/slideshow_info.xml");

       

      /////////////////////////////////////

       

      picInterval = 3000; //Interval between when pic auto switches to the next pic. It's in milliseconds, so 3000 is three seconds.

       

      picTimer = picInterval; //This variable will be compared to getTimer function below

       

      prev_btn.onRelease = function() { //Go to next pic
          prevImage();
      };

       

      next_btn.onRelease = function() { //Go to next pic
          nextImage();
      };

       

      linkto_btn.onPress = function(){ //Open the link in the current window
          if (loaded == filesize) {
            getURL(linkto_txt.text, "_self");
          }
      };

       

      /////////////////////////////////////

       

      p = 0;
      this.onEnterFrame = function() {
          filesize = picture.getBytesTotal();
          loaded = picture.getBytesLoaded();
          preloader._visible = true;
          var percentLoaded:Number = Math.round((picture.getBytesLoaded() / picture.getBytesTotal()) * 100);
      trace("PercentLOADED- "+percentLoaded);
          if (loaded != filesize) {
      trace("NotLOADED- "+loaded+" "+filesize);
              preloader.preload_bar._xscale = 100*loaded/filesize;
              picTimer = getTimer() + picInterval; //Need to keep resetting picTimer ahead while pic is loading, otherwise when a large pic finishes loading it will skip ahead several pics instead of to the next one
          } else {
      trace("YesLOADED- "+loaded+" "+filesize);
              preloader._visible = false;
              if (picture._alpha<100) {
      trace("AlphaPERCENT- "+picture._alpha);
                  picture._alpha += 10;
              }
          }
          if (getTimer() > picTimer) { //Automatically go to the next image after picInterval milliseconds
              nextImage();
          }
      };

       

      ////////////////////////////////////

       

      function displayImage(q) {
          picture._alpha = 0;
          picture.loadMovie(image[q], 1);
          //desc_txt.text = description[q];
          //linkto_txt.text = linkto[q];
          picture_num();
          picTimer = getTimer() + picInterval;
      }

       

      function nextImage() {
          if (p<(total-1)) {
              p++;
          } else { //this else statement to loop from last image back to first
              p=0;
          }
          if (loaded == filesize) {
              displayImage(p);
          }
         
      }

       

      function prevImage() {
          if (p>0) {
              p--;
          } else { //this else statement to loop from 1st to last image
              p=total-1;
          }
          if (loaded == filesize) {
              displayImage(p);
          }
      }

       

      function firstImage() {
          if (loaded == filesize) {
              displayImage(0);
          }
      }

       

      function picture_num() {
          current_pos = p+1;
          pos_txt.text = current_pos+" of "+total;
      }

       

       

       

      Here is what the slideshow/slideshow_info.xml file looks like:

       

      <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      <images>
          <pic>
              <image>slideshow/pic1.jpg</image>
              <linkto>http://www.google.com</linkto>
              <caption>Info Slide #1</caption>
          </pic>
          <pic>
              <image>slideshow/pic2.jpg</image>
              <linkto>http://www.cnn.com</linkto>
              <caption>Info Slide #2</caption>
          </pic>
      </images>

       

      Thanks

        • 1. Re: Can't get getBytesLoaded and getBytesTotal to work properly
          kglad Adobe Community Professional & MVP

          are you trying to display the load-progress of an image or are you trying to display the load-progress of a group of images or something else?

          • 2. Re: Can't get getBytesLoaded and getBytesTotal to work properly
            jshrek Level 1

            I am trying to display the load progress of each image... sorry for not being more clear!

            • 3. Re: Can't get getBytesLoaded and getBytesTotal to work properly
              kglad Adobe Community Professional & MVP

              use:

               

               

              function loadXML(loaded) {
                  if (loaded) {
                      xmlNode = this.firstChild;
                      image = [];
                      linkto = [];
                      description = [];
                      total = xmlNode.childNodes.length;
                      for (i=0; i<total; i++) {
                          image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
                          linkto[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
                          description[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
                      }
                      firstImage();
                  } else {
                      content = "error: xml file not loaded!";
                  }
              }
              xmlData = new XML();
              xmlData.ignoreWhite = true;
              xmlData.onLoad = loadXML;
              xmlData.load("slideshow/slideshow_info.xml");

               

              /////////////////////////////////////

               

              picInterval = 3000; //Interval between when pic auto switches to the next pic. It's in milliseconds, so 3000 is three seconds.

               

              //picTimer = picInterval; //This variable will be compared to getTimer function below

               

              prev_btn.onRelease = function() { //Go to next pic
                  prevImage();
              };

               

              next_btn.onRelease = function() { //Go to next pic
                  nextImage();
              };

               

              linkto_btn.onPress = function(){ //Open the link in the current window
                  if (loaded == filesize) {
                    getURL(linkto_txt.text, "_self");
                  }
              };

               

              /////////////////////////////////////

               

              p = -1;

              nextImage();

              function preloadF() {
                  filesize = picture.getBytesTotal();
                  loaded = picture.getBytesLoaded();
                  preloader._visible = true;
                  var percentLoaded:Number = Math.round(100*loaded/filesize);
                  trace("PercentLOADED- "+percentLoaded);
                  if (loaded != filesize && loaded>100) {
                      trace("NotLOADED- "+loaded+" "+filesize);
                      preloader.preload_bar._xscale = 100*loaded/filesize;
                    
                  } else {
                      trace("YesLOADED- "+loaded+" "+filesize);
                      preloader._visible = false;
                      if (picture._alpha<100) {
                          trace("AlphaPERCENT- "+picture._alpha);
                          picture._alpha += 10;
                      } else {


                          clearInterval(preloadI);

                          setTimeout(nextImage,picInterval);

                      }

                  }


              };

               

              ////////////////////////////////////

               

              function displayImage(q) {
                  picture._alpha = 0;
                  picture.loadMovie(image[q], 1);
                  //desc_txt.text = description[q];
                  //linkto_txt.text = linkto[q];
                  picture_num();

                  clearInterval(preloadI);

                  preloadI=setInterval(preloadF,100);
              }

               

              function nextImage() {
                  if (p<(total-1)) {
                      p++;
                  } else { //this else statement to loop from last image back to first
                      p=0;
                  }
                  if (loaded == filesize) {
                      displayImage(p);
                  }
                 
              }

               

              function prevImage() {
                  if (p>0) {
                      p--;
                  } else { //this else statement to loop from 1st to last image
                      p=total-1;
                  }
                  if (loaded == filesize) {
                      displayImage(p);
                  }
              }

               

              function firstImage() {
                  if (loaded == filesize) {
                      displayImage(0);
                  }
              }

               

              function picture_num() {
                  current_pos = p+1;
                  pos_txt.text = current_pos+" of "+total;
              }

               

               

               

              Here is what the slideshow/slideshow_info.xml file looks like:

               

              <?xml version="1.0" encoding="utf-8" standalone="yes"?>
              <images>
                  <pic>
                      <image>slideshow/pic1.jpg</image>
                      <linkto>http://www.google.com</linkto>
                      <caption>Info Slide #1</caption>
                  </pic>
                  <pic>
                      <image>slideshow/pic2.jpg</image>
                      <linkto>http://www.cnn.com</linkto>
                      <caption>Info Slide #2</caption>
                  </pic>
              </images>

               

              Thanks

              i

              1 person found this helpful
              • 4. Re: Can't get getBytesLoaded and getBytesTotal to work properly
                kglad Adobe Community Professional & MVP

                i just cleaned up your code but you probably only need to embed your preloader font to get it to work.

                1 person found this helpful
                • 5. Re: Can't get getBytesLoaded and getBytesTotal to work properly
                  jshrek Level 1

                  Okay, I will give it a try... thanks!